export type WarehouseViewMode = 'excel' | 'list';

const STORAGE_KEY = 'warehouse-drug-admin-view-mode';

type ViewModeMap = Record<string, WarehouseViewMode>;

function readMap(): ViewModeMap {
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    if (!raw) return {};
    return JSON.parse(raw) as ViewModeMap;
  } catch {
    return {};
  }
}

export function readWarehouseViewMode(productType: number): WarehouseViewMode | null {
  const map = readMap();
  const mode = map[String(productType)];
  return mode === 'excel' || mode === 'list' ? mode : null;
}

export function writeWarehouseViewMode(
  productType: number,
  mode: WarehouseViewMode,
) {
  const map = readMap();
  map[String(productType)] = mode;
  localStorage.setItem(STORAGE_KEY, JSON.stringify(map));
}
