import {
  createPageContext,
  createSearchContext,
  createTableSearchContext,
} from '@hooks';
import {GetStockListParams, OriginalListParams} from '@models';

export const pageContext = createPageContext();
export const searchContext = createSearchContext();

export const contextState: Omit<
  OriginalListParams<GetStockListParams>,
  'groupBy'
> & {groupBy: string[]} = {
  wllbCode: '',
  materialName: '',
  storageLocationName: '',
  wbs: '',
  companyCode: '',
  supplierId: '',
  warehouseId: '',
  wllbClass: '',
  partType: '',
  unitOfMeasurement: '',
  groupBy: [],
};
export const context = createTableSearchContext(contextState);
export function transformState(
  state: typeof contextState,
): OriginalListParams<GetStockListParams> {
  const {
    wllbCode,
    materialName,
    storageLocationName,
    wbs,
    companyCode,
    supplierId,
    warehouseId,
    wllbClass,
    partType,
    unitOfMeasurement,
    groupBy,
  } = state;

  return {
    wllbCode,
    materialName,
    storageLocationName,
    wbs,
    companyCode,
    supplierId,
    warehouseId,
    wllbClass,
    partType,
    unitOfMeasurement,
    groupBy:
      groupBy.length > 0
        ? groupBy.join(',')
        : 'a.storage_location_code,a.material_id,a.wbs,a.account_sleeve',
  };
}
