"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index = require("../../../../hooks/use-locale/index.js");
const index$1 = require("../../../icon/index.js");
const useFormCommonProps = require("../../../form/src/hooks/use-form-common-props.js");
const constants$1 = require("../../../time-picker/src/constants.js");
const constants = require("../constants.js");
const utils = require("../utils.js");
const basicYearTable = require("./basic-year-table.js");
const useRangePicker = require("../composables/use-range-picker.js");
const panelYearRange = require("../props/panel-year-range.js");
const useYearRangeHeader = require("../composables/use-year-range-header.js");
const index$2 = require("../../../../../../@element-plus/icons-vue/dist/index.js");
const Vue = require("vue");
const dayjs_min = require("../../../../../../../_virtual/dayjs.min.js");
const _hoisted_1 = ["disabled", "onClick"];
const _hoisted_2 = ["disabled"];
const _hoisted_3 = ["disabled"];
const _hoisted_4 = ["disabled"];
const _hoisted_5 = ["disabled"];
const step = 10;
const unit = "year";
var panel_year_range_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ Vue.defineComponent({
  name: "DatePickerYearRange",
  __name: "panel-year-range",
  props: panelYearRange.panelYearRangeProps,
  emits: panelYearRange.panelYearRangeEmits,
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { lang } = index.useLocale();
    const leftDate = Vue.ref(dayjs_min.default().locale(lang.value));
    const rightDate = Vue.ref(dayjs_min.default().locale(lang.value).add(step, unit));
    const isDefaultFormat = Vue.inject(constants.ROOT_PICKER_IS_DEFAULT_FORMAT_INJECTION_KEY, void 0);
    const pickerBase = Vue.inject(constants$1.PICKER_BASE_INJECTION_KEY);
    const { shortcuts, disabledDate, cellClassName } = pickerBase.props;
    const format = Vue.toRef(pickerBase.props, "format");
    const defaultValue = Vue.toRef(pickerBase.props, "defaultValue");
    const { minDate, maxDate, rangeState, ppNs, drpNs, handleChangeRange, handleRangeConfirm, handleShortcutClick, onSelect, parseValue } = useRangePicker.useRangePicker(props, {
      defaultValue,
      leftDate,
      rightDate,
      step,
      unit,
      sortDates
    });
    const { leftPrevYear, rightNextYear, leftNextYear, rightPrevYear, leftLabel, rightLabel, leftYear, rightYear } = useYearRangeHeader.useYearRangeHeader({
      unlinkPanels: Vue.toRef(props, "unlinkPanels"),
      leftDate,
      rightDate
    });
    const yearRangeDisabled = useFormCommonProps.useFormDisabled();
    const hasShortcuts = Vue.computed(() => !!shortcuts.length);
    const panelKls = Vue.computed(() => [
      ppNs.b(),
      drpNs.b(),
      ppNs.is("border", props.border),
      ppNs.is("disabled", yearRangeDisabled.value),
      { "has-sidebar": Boolean(Vue.useSlots().sidebar) || hasShortcuts.value }
    ]);
    const leftPanelKls = Vue.computed(() => {
      return {
        content: [
          ppNs.e("content"),
          drpNs.e("content"),
          "is-left"
        ],
        arrowLeftBtn: [ppNs.e("icon-btn"), "d-arrow-left"],
        arrowRightBtn: [
          ppNs.e("icon-btn"),
          ppNs.is("disabled", !enableYearArrow.value || yearRangeDisabled.value),
          "d-arrow-right"
        ]
      };
    });
    const rightPanelKls = Vue.computed(() => {
      return {
        content: [
          ppNs.e("content"),
          drpNs.e("content"),
          "is-right"
        ],
        arrowLeftBtn: [
          ppNs.e("icon-btn"),
          ppNs.is("disabled", !enableYearArrow.value || yearRangeDisabled.value),
          "d-arrow-left"
        ],
        arrowRightBtn: [ppNs.e("icon-btn"), "d-arrow-right"]
      };
    });
    const enableYearArrow = Vue.computed(() => {
      return props.unlinkPanels && rightYear.value > leftYear.value + 1;
    });
    const handleRangePick = (val, close = true) => {
      const minDate_ = val.minDate;
      const maxDate_ = val.maxDate;
      if (maxDate.value === maxDate_ && minDate.value === minDate_) return;
      emit("calendar-change", [minDate_.toDate(), maxDate_ && maxDate_.toDate()]);
      maxDate.value = maxDate_;
      minDate.value = minDate_;
      if (!close) return;
      handleRangeConfirm();
    };
    const parseUserInput = (value) => {
      return utils.correctlyParseUserInput(value, format.value, lang.value, isDefaultFormat);
    };
    const isValidValue = (date) => {
      return utils.isValidRange(date) && (disabledDate ? !disabledDate(date[0].toDate()) && !disabledDate(date[1].toDate()) : true);
    };
    const handleClear = () => {
      let valueOnClear = null;
      if (pickerBase == null ? void 0 : pickerBase.emptyValues) valueOnClear = pickerBase.emptyValues.valueOnClear.value;
      const defaultArr = utils.getDefaultValue(Vue.unref(defaultValue), {
        lang: Vue.unref(lang),
        step,
        unit,
        unlinkPanels: props.unlinkPanels
      });
      leftDate.value = defaultArr[0];
      rightDate.value = defaultArr[1];
      emit("pick", valueOnClear);
    };
    function sortDates(minDate2, maxDate2) {
      if (props.unlinkPanels && maxDate2) {
        const minDateYear = (minDate2 == null ? void 0 : minDate2.year()) || 0;
        const maxDateYear = maxDate2.year();
        rightDate.value = minDateYear + step > maxDateYear ? maxDate2.add(step, unit) : maxDate2;
      } else rightDate.value = leftDate.value.add(step, unit);
    }
    Vue.watch(() => props.visible, (visible) => {
      if (!visible && rangeState.value.selecting) {
        parseValue(props.parsedValue);
        onSelect(false);
      }
    });
    emit("set-picker-option", ["isValidValue", isValidValue]);
    emit("set-picker-option", ["parseUserInput", parseUserInput]);
    emit("set-picker-option", ["handleClear", handleClear]);
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createElementBlock("div", { class: Vue.normalizeClass(panelKls.value) }, [Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(ppNs).e("body-wrapper")) }, [
        Vue.renderSlot(_ctx.$slots, "sidebar", { class: Vue.normalizeClass(Vue.unref(ppNs).e("sidebar")) }),
        hasShortcuts.value ? (Vue.openBlock(), Vue.createElementBlock("div", {
          key: 0,
          class: Vue.normalizeClass(Vue.unref(ppNs).e("sidebar"))
        }, [(Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(Vue.unref(shortcuts), (shortcut, key) => {
          return Vue.openBlock(), Vue.createElementBlock("button", {
            key,
            type: "button",
            class: Vue.normalizeClass(Vue.unref(ppNs).e("shortcut")),
            disabled: Vue.unref(yearRangeDisabled),
            onClick: ($event) => Vue.unref(handleShortcutClick)(shortcut)
          }, Vue.toDisplayString(shortcut.text), 11, _hoisted_1);
        }), 128))], 2)) : Vue.createCommentVNode("v-if", true),
        Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(ppNs).e("body")) }, [Vue.createElementVNode("div", { class: Vue.normalizeClass(leftPanelKls.value.content) }, [Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(drpNs).e("header")) }, [
          Vue.createElementVNode("button", {
            type: "button",
            class: Vue.normalizeClass(leftPanelKls.value.arrowLeftBtn),
            disabled: Vue.unref(yearRangeDisabled),
            onClick: _cache[0] || (_cache[0] = (...args) => Vue.unref(leftPrevYear) && Vue.unref(leftPrevYear)(...args))
          }, [Vue.renderSlot(_ctx.$slots, "prev-year", {}, () => [Vue.createVNode(Vue.unref(index$1.ElIcon), null, {
            default: Vue.withCtx(() => [Vue.createVNode(Vue.unref(index$2.DArrowLeft))]),
            _: 1
          })])], 10, _hoisted_2),
          _ctx.unlinkPanels ? (Vue.openBlock(), Vue.createElementBlock("button", {
            key: 0,
            type: "button",
            disabled: !enableYearArrow.value || Vue.unref(yearRangeDisabled),
            class: Vue.normalizeClass(leftPanelKls.value.arrowRightBtn),
            onClick: _cache[1] || (_cache[1] = (...args) => Vue.unref(leftNextYear) && Vue.unref(leftNextYear)(...args))
          }, [Vue.renderSlot(_ctx.$slots, "next-year", {}, () => [Vue.createVNode(Vue.unref(index$1.ElIcon), null, {
            default: Vue.withCtx(() => [Vue.createVNode(Vue.unref(index$2.DArrowRight))]),
            _: 1
          })])], 10, _hoisted_3)) : Vue.createCommentVNode("v-if", true),
          Vue.createElementVNode("div", null, Vue.toDisplayString(Vue.unref(leftLabel)), 1)
        ], 2), Vue.createVNode(basicYearTable.default, {
          "selection-mode": "range",
          date: leftDate.value,
          "min-date": Vue.unref(minDate),
          "max-date": Vue.unref(maxDate),
          "range-state": Vue.unref(rangeState),
          "disabled-date": Vue.unref(disabledDate),
          disabled: Vue.unref(yearRangeDisabled),
          "cell-class-name": Vue.unref(cellClassName),
          onChangerange: Vue.unref(handleChangeRange),
          onPick: handleRangePick,
          onSelect: Vue.unref(onSelect)
        }, null, 8, [
          "date",
          "min-date",
          "max-date",
          "range-state",
          "disabled-date",
          "disabled",
          "cell-class-name",
          "onChangerange",
          "onSelect"
        ])], 2), Vue.createElementVNode("div", { class: Vue.normalizeClass(rightPanelKls.value.content) }, [Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(drpNs).e("header")) }, [
          _ctx.unlinkPanels ? (Vue.openBlock(), Vue.createElementBlock("button", {
            key: 0,
            type: "button",
            disabled: !enableYearArrow.value || Vue.unref(yearRangeDisabled),
            class: Vue.normalizeClass(rightPanelKls.value.arrowLeftBtn),
            onClick: _cache[2] || (_cache[2] = (...args) => Vue.unref(rightPrevYear) && Vue.unref(rightPrevYear)(...args))
          }, [Vue.renderSlot(_ctx.$slots, "prev-year", {}, () => [Vue.createVNode(Vue.unref(index$1.ElIcon), null, {
            default: Vue.withCtx(() => [Vue.createVNode(Vue.unref(index$2.DArrowLeft))]),
            _: 1
          })])], 10, _hoisted_4)) : Vue.createCommentVNode("v-if", true),
          Vue.createElementVNode("button", {
            type: "button",
            class: Vue.normalizeClass(rightPanelKls.value.arrowRightBtn),
            disabled: Vue.unref(yearRangeDisabled),
            onClick: _cache[3] || (_cache[3] = (...args) => Vue.unref(rightNextYear) && Vue.unref(rightNextYear)(...args))
          }, [Vue.renderSlot(_ctx.$slots, "next-year", {}, () => [Vue.createVNode(Vue.unref(index$1.ElIcon), null, {
            default: Vue.withCtx(() => [Vue.createVNode(Vue.unref(index$2.DArrowRight))]),
            _: 1
          })])], 10, _hoisted_5),
          Vue.createElementVNode("div", null, Vue.toDisplayString(Vue.unref(rightLabel)), 1)
        ], 2), Vue.createVNode(basicYearTable.default, {
          "selection-mode": "range",
          date: rightDate.value,
          "min-date": Vue.unref(minDate),
          "max-date": Vue.unref(maxDate),
          "range-state": Vue.unref(rangeState),
          "disabled-date": Vue.unref(disabledDate),
          disabled: Vue.unref(yearRangeDisabled),
          "cell-class-name": Vue.unref(cellClassName),
          onChangerange: Vue.unref(handleChangeRange),
          onPick: handleRangePick,
          onSelect: Vue.unref(onSelect)
        }, null, 8, [
          "date",
          "min-date",
          "max-date",
          "range-state",
          "disabled-date",
          "disabled",
          "cell-class-name",
          "onChangerange",
          "onSelect"
        ])], 2)], 2)
      ], 2)], 2);
    };
  }
});
exports.default = panel_year_range_vue_vue_type_script_setup_true_lang_default;
