'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../hooks/index.cjs');
require('../../../locale/index.cjs');
require('../../../vue-components/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
require('../../locale-provider/index.cjs');
var index = require('../style/index.cjs');
var Header = require('./Header.cjs');
var index$1 = require('../../../hooks/use-merged-state/index.cjs');
var en_US = require('../../../locale/lang/en_US.cjs');
var useLocaleReceiver = require('../../locale-provider/src/useLocaleReceiver.cjs');
var _class = require('../../../utils/dom/class.cjs');
var PickerPanel = require('../../../vue-components/vc-picker/src/PickerPanel.cjs');

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__ */ vue.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.default("picker", props);
      const [wrapSSR, hashId] = index.default(prefixCls);
      const calendarPrefixCls = vue.computed(() => `${prefixCls.value}-calendar`);
      const maybeToString = (date) => {
        return props.valueFormat ? generateConfig.toString(date, props.valueFormat) : date;
      };
      const value = vue.computed(() => {
        if (props.value) {
          return props.valueFormat ? generateConfig.toDate(props.value, props.valueFormat) : props.value;
        }
        return props.value === "" ? void 0 : props.value;
      });
      const defaultValue = vue.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] = index$1.useMergedState(() => value.value || generateConfig.getNow(), {
        defaultValue: defaultValue.value,
        value
      });
      const [mergedMode, setMergedMode] = index$1.useMergedState("month", {
        value: vue.toRef(props, "mode")
      });
      const panelMode = vue.computed(() => mergedMode.value === "year" ? "month" : "date");
      const mergedDisabledDate = vue.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 = vue.computed(() => {
        const {
          locale
        } = props;
        const result = {
          ...en_US.default.DatePicker,
          ...locale
        };
        result.lang = {
          ...result.lang,
          ...(locale || {}).lang
        };
        return result;
      });
      const [mergedLocale] = useLocaleReceiver.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 vue.createVNode("div", {
            "class": _class.classNames(`${prefixCls.value}-cell-inner`, `${calendarPrefixCls.value}-date`, {
              [`${calendarPrefixCls.value}-date-today`]: isSameDate(today, date)
            })
          }, [vue.createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-value`
          }, [String(generateConfig.getDate(date)).padStart(2, "0")]), vue.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 vue.createVNode("div", {
            "class": _class.classNames(`${prefixCls.value}-cell-inner`, `${calendarPrefixCls.value}-date`, {
              [`${calendarPrefixCls.value}-date-today`]: isSameMonth(today, date)
            })
          }, [vue.createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-value`
          }, [months[generateConfig.getMonth(date)]]), vue.createVNode("div", {
            "class": `${calendarPrefixCls.value}-date-content`
          }, [monthCellRender && monthCellRender({
            current: date
          })])]);
        };
        return wrapSSR(vue.createVNode("div", {
          ...attrs,
          "class": _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
        }) : vue.createVNode(Header.default, {
          "prefixCls": calendarPrefixCls.value,
          "value": mergedValue.value,
          "generateConfig": generateConfig,
          "mode": mergedMode.value,
          "fullscreen": fullscreen,
          "locale": mergedLocale.value.lang,
          "validRange": validRange,
          "onChange": onInternalSelect,
          "onModeChange": triggerModeChange
        }, null), vue.createVNode(PickerPanel.default, {
          "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;
}

exports.default = generateCalendar;
//# sourceMappingURL=generateCalendar.cjs.map
