import * as yup from 'yup';

export type FinanceTableFilterType = {
  search: string;
  transaction_types: string;
  bank_ids: string;
  customer_ids: string;
  supplier_ids: string;
  sort_by: string;
  start_date: string;
  end_date: string;
};

export const FinanceTableFilterSchema = yup.object({
  search: yup.string().optional(),
  transaction_types: yup.string().optional(),
  bank_ids: yup.string().optional(),
  customer_ids: yup.string().optional(),
  supplier_ids: yup.string().optional(),
  sort_by: yup.string().optional(),
  start_date: yup.string().optional(),
  end_date: yup
    .string()
    .optional()
    .test(
      'is-greater-than-start',
      'Tanggal akhir tidak boleh masa lampau',
      function (value) {
        const { start_date } = this.parent;
        if (!start_date || !value) return true;
        return new Date(value) >= new Date(start_date);
      }
    ),
}) as yup.ObjectSchema<FinanceTableFilterType>;

export type FinanceTableFilterValues = yup.InferType<
  typeof FinanceTableFilterSchema
>;
