import type { MemberFilterState, MemberPaginationState } from '../../types';

import { createDefaultFilters } from './constants';

interface FilterActionOptions {
  filters: MemberFilterState;
  loadListAndStats: () => Promise<void>;
  pagination: MemberPaginationState;
}

/**
 * 文件职责：会员列表筛选与分页动作。
 */
export function createFilterActions(options: FilterActionOptions) {
  function setKeyword(value: string) {
    options.filters.keyword = value;
  }

  function setTierId(value: string) {
    options.filters.tierId = value || '';
  }

  async function handleSearch() {
    options.pagination.page = 1;
    await options.loadListAndStats();
  }

  async function handleReset() {
    const defaults = createDefaultFilters();
    options.filters.keyword = defaults.keyword;
    options.filters.tierId = defaults.tierId;
    options.pagination.page = 1;
    await options.loadListAndStats();
  }

  async function handlePageChange(page: number, pageSize: number) {
    options.pagination.page = page;
    options.pagination.pageSize = pageSize;
    await options.loadListAndStats();
  }

  return {
    handlePageChange,
    handleReset,
    handleSearch,
    setKeyword,
    setTierId,
  };
}
