"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../../../../hooks/use-locale/index.js");
const constants = require("../../../time-picker/src/constants.js");
const utils = require("../utils.js");
const Vue = require("vue");
const usePanelDateRange = (props, emit, leftDate, rightDate) => {
  const leftCurrentView = Vue.ref("date");
  const leftCurrentViewRef = Vue.ref();
  const rightCurrentView = Vue.ref("date");
  const rightCurrentViewRef = Vue.ref();
  const { disabledDate } = Vue.inject(constants.PICKER_BASE_INJECTION_KEY).props;
  const { t, lang } = index.useLocale();
  const leftYear = Vue.computed(() => {
    return leftDate.value.year();
  });
  const leftMonth = Vue.computed(() => {
    return leftDate.value.month();
  });
  const rightYear = Vue.computed(() => {
    return rightDate.value.year();
  });
  const rightMonth = Vue.computed(() => {
    return rightDate.value.month();
  });
  function computedYearLabel(currentView, yearValue) {
    const yearTranslation = t("el.datepicker.year");
    if (currentView.value === "year") {
      const startYear = Math.floor(yearValue.value / 10) * 10;
      return yearTranslation ? `${startYear} ${yearTranslation} - ${startYear + 9} ${yearTranslation}` : `${startYear} - ${startYear + 9}`;
    }
    return `${yearValue.value} ${yearTranslation}`;
  }
  function focusPicker(currentViewRef) {
    currentViewRef == null ? void 0 : currentViewRef.focus();
  }
  async function showPicker(pickerType, view) {
    if (props.disabled) return;
    const currentView = pickerType === "left" ? leftCurrentView : rightCurrentView;
    const currentViewRef = pickerType === "left" ? leftCurrentViewRef : rightCurrentViewRef;
    currentView.value = view;
    await Vue.nextTick();
    focusPicker(currentViewRef.value);
  }
  async function handlePick(mode, pickerType, value) {
    if (props.disabled) return;
    const isLeftPicker = pickerType === "left";
    const startDate = isLeftPicker ? leftDate : rightDate;
    const endDate = isLeftPicker ? rightDate : leftDate;
    const currentView = isLeftPicker ? leftCurrentView : rightCurrentView;
    const currentViewRef = isLeftPicker ? leftCurrentViewRef : rightCurrentViewRef;
    if (mode === "year") startDate.value = utils.getValidDateOfYear(startDate.value.year(value), lang.value, disabledDate);
    if (mode === "month") startDate.value = utils.getValidDateOfMonth(startDate.value, startDate.value.year(), value, lang.value, disabledDate);
    if (!props.unlinkPanels) endDate.value = pickerType === "left" ? startDate.value.add(1, "month") : startDate.value.subtract(1, "month");
    currentView.value = mode === "year" ? "month" : "date";
    await Vue.nextTick();
    focusPicker(currentViewRef.value);
    handlePanelChange(mode);
  }
  function handlePanelChange(mode) {
    emit("panel-change", [leftDate.value.toDate(), rightDate.value.toDate()], mode);
  }
  function adjustDateByView(currentView, date, forward) {
    const action = forward ? "add" : "subtract";
    return currentView === "year" ? date[action](10, "year") : date[action](1, "year");
  }
  return {
    leftCurrentView,
    rightCurrentView,
    leftCurrentViewRef,
    rightCurrentViewRef,
    leftYear,
    rightYear,
    leftMonth,
    rightMonth,
    leftYearLabel: Vue.computed(() => computedYearLabel(leftCurrentView, leftYear)),
    rightYearLabel: Vue.computed(() => computedYearLabel(rightCurrentView, rightYear)),
    showLeftPicker: (view) => showPicker("left", view),
    showRightPicker: (view) => showPicker("right", view),
    handleLeftYearPick: (year) => handlePick("year", "left", year),
    handleRightYearPick: (year) => handlePick("year", "right", year),
    handleLeftMonthPick: (month) => handlePick("month", "left", month),
    handleRightMonthPick: (month) => handlePick("month", "right", month),
    handlePanelChange,
    adjustDateByView
  };
};
exports.usePanelDateRange = usePanelDateRange;
