import Taro from '@tarojs/taro';
import type { ShopStore } from '@/api/shop/shopStore/model';

export const SELECTED_STORE_STORAGE_KEY = 'SelectedStore';

export function getSelectedStoreFromStorage(): ShopStore | null {
  try {
    const raw = Taro.getStorageSync(SELECTED_STORE_STORAGE_KEY);
    if (!raw) return null;
    return (typeof raw === 'string' ? JSON.parse(raw) : raw) as ShopStore;
  } catch (_e) {
    return null;
  }
}

export function saveSelectedStoreToStorage(store: ShopStore | null) {
  if (!store) {
    Taro.removeStorageSync(SELECTED_STORE_STORAGE_KEY);
    return;
  }
  Taro.setStorageSync(SELECTED_STORE_STORAGE_KEY, store);
}

export function getSelectedStoreIdFromStorage(): number | undefined {
  return getSelectedStoreFromStorage()?.id;
}

