import { OptionType } from '@/components/input/SelectInput';
import * as yup from 'yup';

export type DebtSupplierFilterType = {
  startDate: string | null | undefined;
  endDate: string | null | undefined;
  supplierIds: OptionType[] | null | undefined;
  filterBy: OptionType | null | undefined;
};

export const DebtSupplierFilterSchema: yup.ObjectSchema<DebtSupplierFilterType> =
  yup.object({
    startDate: yup.string().optional().notRequired(),
    endDate: yup
      .string()
      .optional()
      .notRequired()
      .test(
        'is-greater-than-start',
        'Tanggal akhir tidak boleh masa lampau',
        function (value) {
          const startDate = this.parent.startDate;
          if (!startDate || !value) return true;
          return new Date(value) >= new Date(startDate);
        }
      ),
    supplierIds: yup
      .array()
      .of(
        yup.object({
          value: yup.mixed<string | number>().required(),
          label: yup.string().required(),
        })
      )
      .optional()
      .notRequired(),
    filterBy: yup
      .object({
        value: yup.mixed<string | number>().required(),
        label: yup.string().required(),
      })
      .optional()
      .notRequired(),
  });

export type DebtSupplierFilterValues = yup.InferType<
  typeof DebtSupplierFilterSchema
>;
