import type { FinanceCostFilterState, FinanceCostTabKey } from '../../types';

/**
 * 文件职责：成本管理页面筛选项与月份切换动作。
 */
import type { FinanceCostDimension } from '#/api/finance/cost';

import { getCurrentMonthString, shiftMonth } from './helpers';

interface FilterActionOptions {
  activeTab: { value: FinanceCostTabKey };
  filters: FinanceCostFilterState;
}

/** 创建筛选动作。 */
export function createFilterActions(options: FilterActionOptions) {
  function setActiveTab(value: FinanceCostTabKey) {
    options.activeTab.value = value;
  }

  function setDimension(value: FinanceCostDimension) {
    options.filters.dimension = value;
  }

  function setStoreId(value: string) {
    options.filters.storeId = value;
  }

  function setMonth(value: string) {
    options.filters.month = value || getCurrentMonthString();
  }

  function shiftCurrentMonth(offset: number) {
    options.filters.month = shiftMonth(options.filters.month, offset);
  }

  function setPreviousMonth() {
    shiftCurrentMonth(-1);
  }

  function setNextMonth() {
    shiftCurrentMonth(1);
  }

  return {
    setActiveTab,
    setDimension,
    setMonth,
    setNextMonth,
    setPreviousMonth,
    setStoreId,
  };
}
