/** 赛事列表 UI 状态（返回列表时恢复筛选与分页） */

const STORAGE_KEY = 'admin_matches_list_ui';

export type MatchesListUiState = {
  page: number;
  pageSize: number;
  filterStatus: string;
  keyword: string;
};

function defaultState(): MatchesListUiState {
  return {
    page: 1,
    pageSize: 10,
    filterStatus: '',
    keyword: '',
  };
}

export function readMatchesListUiState(): MatchesListUiState | null {
  try {
    const raw = sessionStorage.getItem(STORAGE_KEY);
    if (!raw) return null;
    return JSON.parse(raw) as MatchesListUiState;
  } catch {
    return null;
  }
}

export function writeMatchesListUiState(state: MatchesListUiState) {
  sessionStorage.setItem(STORAGE_KEY, JSON.stringify(state));
}

export function patchMatchesListUiState(patch: Partial<MatchesListUiState>) {
  const base = readMatchesListUiState() ?? defaultState();
  writeMatchesListUiState({ ...base, ...patch });
}
