import type { FactoryOrderSearchQueryVisibilityContext } from '@/types/api/factoryOrderNew/searchQueryVisibility'

export const FACTORY_ORDER_LIST_SEARCH_FIELD_RULES: Record<
  string,
  (ctx: FactoryOrderSearchQueryVisibilityContext) => boolean
> = {
  batchArrangeNumber: (c) => c.isCardLayout,
  operationNo: (c) => c.isCardLayout,
  receiverCountry: (c) => !c.isCardLayout,
  logisticsWayId: (c) => c.status === 'ALL',
  customerNo: (c) => c.status === 'ALL',
}

export function filterFactoryOrderListSearchPayload(
  payload: Record<string, unknown>,
  ctx: FactoryOrderSearchQueryVisibilityContext,
): Record<string, unknown> {
  const out = { ...payload }
  for (const [key, rule] of Object.entries(
    FACTORY_ORDER_LIST_SEARCH_FIELD_RULES,
  )) {
    if (!Object.prototype.hasOwnProperty.call(out, key)) continue
    if (!rule(ctx)) {
      delete out[key]
    }
  }
  return out
}
