import { defineComponent, computed, toRef, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import '../../../locale/index.mjs';
import '../../../vue-components/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import '../../locale-provider/index.mjs';
import useStyle from '../style/index.mjs';
import CalendarHeader from './Header.mjs';
import { useMergedState } from '../../../hooks/use-merged-state/index.mjs';
import localeValues from '../../../locale/lang/en_US.mjs';
import { useLocaleReceiver } from '../../locale-provider/src/useLocaleReceiver.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import VcPickerPanel from '../../../vue-components/vc-picker/src/PickerPanel.mjs';

function generateCalendar(generateConfig) {
  function isSameYear(date1, date2) {
    return date1 && date2 && generateConfig.getYear(date1) === generateConfig.getYear(date2);
  }
  function isSameMonth(date1, date2) {
    return isSameYear(date1, date2) && generateConfig.getMonth(date1) === generateConfig.getMonth(date2);
  }
  function isSameDate(date1, date2) {
    return isSameMonth(date1, date2) && generateConfig.getDate(date1) === generateConfig.getDate(date2);
  }
  const Calendar = /* @__PURE__ */ defineComponent({
    name: "ACalendar",
    inheritAttrs: false,
    props: {
      "prefixCls": String,
      "locale": {
        type: Object,
        default: void 0
      },
      "validRange": {
        type: Array,
        default: void 0
      },
      "disabledDate": {
        type: Function,
        default: void 0
      },
      "dateFullCellRender": {
        type: Function,
        default: void 0
      },
      "dateCellRender": {
        type: Function,
        default: void 0
      },
      "monthFullCellRender": {
        type: Function,
        default: void 0
      },
      "monthCellRender": {
        type: Function,
        default: void 0
      },
      "headerRender": {
        type: Function,
        default: void 0
      },
      "value": {
        type: [Object, String],
        default: void 0
      },
      "defaultValue": {
        type: [Object, String],
        default: void 0
      },
      "mode": {
        type: String,
        default: void 0
      },
      "fullscreen": {
        type: Boolean,
        default: void 0
      },
      "onChange": {
        type: Function,
        default: void 0
      },
      "onUpdate:value": {
        type: Function,
        default: void 0
      },
      "onPanelChange": {
        type: Function,
        default: void 0
      },
      "onSelect": {
        type: Function,
        default: void 0
      },
      "valueFormat": {
        type: String,
        default: void 0
      }
    },
    slots: Object,
    setup(p, {
      emit,
      slots,
      attrs
    }) {
      const props = p;
      const {
        prefixCls,
        direction
      } = useConfigInject("picker", props);
      const [wrapSSR, hashId] = useStyle(prefixCls);
      const calendarPrefixCls = computed(() => `${prefixCls.value}-calendar`);
      const maybeToString = (date) => {
        return props.valueFormat ? generateConfig.toString(date, props.valueFormat) : date;
      };
      const value = computed(() => {
        if (props.value) {
          return props.valueFormat ? generateConfig.toDate(props.value, props.valueFormat) : props.value;
        }
        return props.value === "" ? void 0 : props.value;
      });
      const defaultValue = computed(() => {
        if (props.defaultValue) {
          return props.valueFormat ? generateConfig.toDate(props.defaultValue, props.valueFormat) : props.defaultValue;
        }
        return props.defaultValue === "" ? void 0 : props.defaultValue;
      });
      const [mergedValue, setMergedValue] = useMergedState(() => value.value || generateConfig.getNow(), {
        defaultValue: defaultValue.value,
        value
      });
      const [mergedMode, setMergedMode] = useMergedState("month", {
        value: toRef(props, "mode")
      });
      const panelMode = computed(() => mergedMode.value === "year" ? "month" : "date");
      const mergedDisabledDate = computed(() => {
        return (date) => {
          var _a;
          const notInRange = props.validRange ? generateConfig.isAfter(props.validRange[0], date) || generateConfig.isAfter(date, props.validRange[1]) : false;
          return notInRange || !!((_a = props.disabledDate) == null ? void 0 : _a.call(props, date));
        };
      });
      const triggerPanelChange = (date, newMode) => {
        emit("panelChange", maybeToString(date), newMode);
      };
      const triggerChange = (date) => {
        setMergedValue(date);
        if (!isSameDate(date, mergedValue.value)) {
          if (panelMode.value === "date" && !isSameMonth(date, mergedValue.value) || panelMode.value === "month" && !isSameYear(date, mergedValue.value))
            triggerPanelChange(date, mergedMode.value);
          const val = maybeToString(date);
          emit("update:value", val);
          emit("change", val);
        }
      };
      const triggerModeChange = (newMode) => {
        setMergedMode(newMode);
        triggerPanelChange(mergedValue.value, newMode);
      };
      const onInternalSelect = (date, source) => {
        triggerChange(date);
        emit("select", maybeToString(date), {
          source
        });
      };
      const defaultLocale = computed(() => {
        const {
          locale
        } = props;
        const result = {
          ...localeValues.DatePicker,
          ...locale
        };
        result.lang = {
          ...result.lang,
          ...(locale || {}).lang
        };
        return result;
      });
      const [mergedLocale] = useLocaleReceiver("Calendar", defaultLocale);
      return () => {
        const today = generateConfig.getNow();
        const {
          dateFullCellRender = slots == null ? void 0 : slots.dateFullCellRender,
          dateCellRender = slots == null ? void 0 : slots.dateCellRender,
          monthFullCellRender = slots == null ? void 0 : slots.monthFullCellRender,
          monthCellRender = slots == null ? void 0 : slots.monthCellRender,
          headerRender = slots == null ? void 0 : slots.headerRender,
          fullscreen = true,
          validRange
        } = props;
        const dateRender = ({
          current: date
        }) => {
          if (dateFullCellRender)
            return dateFullCellRender({
              current: date
            });
          return createVNode("div", {
            "class": classNames(`${prefixCls.value}-cell-inner`, `${calendarPrefixCls.value}-date`, {
              [`${calendarPrefixCls.value}-date-today`]: isSameDate(today, date)
            })
          }, [createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-value`
          }, [String(generateConfig.getDate(date)).padStart(2, "0")]), createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-content`
          }, [dateCellRender && dateCellRender({
            current: date
          })])]);
        };
        const monthRender = ({
          current: date
        }, locale) => {
          if (monthFullCellRender)
            return monthFullCellRender({
              current: date
            });
          const months = locale.shortMonths || generateConfig.locale.getShortMonths(locale.locale);
          return createVNode("div", {
            "class": classNames(`${prefixCls.value}-cell-inner`, `${calendarPrefixCls.value}-date`, {
              [`${calendarPrefixCls.value}-date-today`]: isSameMonth(today, date)
            })
          }, [createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-value`
          }, [months[generateConfig.getMonth(date)]]), createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-content`
          }, [monthCellRender && monthCellRender({
            current: date
          })])]);
        };
        return wrapSSR(createVNode("div", {
          ...attrs,
          "class": classNames(calendarPrefixCls.value, {
            [`${calendarPrefixCls.value}-full`]: fullscreen,
            [`${calendarPrefixCls.value}-mini`]: !fullscreen,
            [`${calendarPrefixCls.value}-rtl`]: direction.value === "rtl"
          }, attrs.class, hashId.value)
        }, [headerRender ? headerRender({
          value: mergedValue.value,
          type: mergedMode.value,
          onChange: (nextDate) => {
            onInternalSelect(nextDate, "customize");
          },
          onTypeChange: triggerModeChange
        }) : createVNode(CalendarHeader, {
          "prefixCls": calendarPrefixCls.value,
          "value": mergedValue.value,
          "generateConfig": generateConfig,
          "mode": mergedMode.value,
          "fullscreen": fullscreen,
          "locale": mergedLocale.value.lang,
          "validRange": validRange,
          "onChange": onInternalSelect,
          "onModeChange": triggerModeChange
        }, null), createVNode(VcPickerPanel, {
          "value": mergedValue.value,
          "prefixCls": prefixCls.value,
          "locale": mergedLocale.value.lang,
          "generateConfig": generateConfig,
          "dateRender": dateRender,
          "monthCellRender": (obj) => monthRender(obj, mergedLocale.value.lang),
          "onSelect": (nextDate) => {
            onInternalSelect(nextDate, panelMode.value);
          },
          "mode": panelMode.value,
          "picker": panelMode.value,
          "disabledDate": mergedDisabledDate.value,
          "hideHeader": true
        }, null)]));
      };
    }
  });
  Calendar.install = function(app) {
    app.component(Calendar.name, Calendar);
    return app;
  };
  return Calendar;
}

export { generateCalendar as default };
//# sourceMappingURL=generateCalendar.mjs.map
