/**
 * ldesign v0.2.3
 * (c) 2022年9月24日 swimly
 * @license MIT
 */

import dayjs from 'dayjs';
import isObject from 'lodash/isObject';

function useDisableDate(props) {
  return {
    disableDate: value => !isEnabled({
      disableDate: props.disableDate,
      format: props.format,
      mode: props.mode,
      value
    }),
    minDate: isObject(props.disableDate) && "before" in props.disableDate ? new Date(props.disableDate.before) : props.start,
    maxDate: isObject(props.disableDate) && "after" in props.disableDate ? new Date(props.disableDate.after) : props.end
  };
}

function isEnabled(props) {
  if (!props.disableDate) return true;
  let isEnabled2 = true;

  if (typeof props.disableDate === "function") {
    return !props.disableDate(props.value);
  }

  if (Array.isArray(props.disableDate)) {
    let isIncludes = false;
    const formattedDisabledDate = props.disableDate.map(item => dayjs(item, props.format));
    formattedDisabledDate.forEach(item => {
      if (item.isSame(dayjs(props.value))) {
        isIncludes = true;
      }
    });
    return !isIncludes;
  }

  if (props.disableDate.from && props.disableDate.to) {
    const compareMin = dayjs(new Date(props.disableDate.from));
    const compareMax = dayjs(new Date(props.disableDate.to));
    return !dayjs(props.value).isBetween(compareMin, compareMax, props.mode, "[]");
  }

  const min = props.disableDate.before ? new Date(props.disableDate.before) : null;
  const max = props.disableDate.after ? new Date(props.disableDate.after) : null;

  if (max && min) {
    const compareMin = dayjs(new Date(min));
    const compareMax = dayjs(new Date(max));
    isEnabled2 = dayjs(props.value).isBetween(compareMin, compareMax, props.mode, "[]");
  } else if (min) {
    const compareMin = dayjs(new Date(min));
    isEnabled2 = !dayjs(props.value).isBefore(compareMin, props.mode);
  } else if (max) {
    const compareMax = dayjs(new Date(max));
    isEnabled2 = !dayjs(props.value).isAfter(compareMax, props.mode);
  }

  return isEnabled2;
}

export { useDisableDate as default };
//# sourceMappingURL=use-disabled-date.js.map
