"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("../../../../constants/event.js");
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
const index = require("../../../../hooks/use-locale/index.js");
const utils = require("../utils.js");
const vue = require("vue");
const isEqual = require("../../../../../../lodash-es/isEqual.js");
const useCommonPicker = (props, emit) => {
  const { lang } = index.useLocale();
  const pickerVisible = vue.ref(false);
  const pickerActualVisible = vue.ref(false);
  const userInput = vue.ref(null);
  const valueIsEmpty = vue.computed(() => {
    const { modelValue } = props;
    return !modelValue || shared_esmBundler.isArray(modelValue) && !modelValue.filter(Boolean).length;
  });
  const emitInput = (input) => {
    if (!utils.valueEquals(props.modelValue, input)) {
      let formatted;
      if (shared_esmBundler.isArray(input)) formatted = input.map((item) => utils.formatter(item, props.valueFormat, lang.value));
      else if (input) formatted = utils.formatter(input, props.valueFormat, lang.value);
      emit(event.UPDATE_MODEL_EVENT, input ? formatted : input, lang.value);
    }
  };
  const parsedValue = vue.computed(() => {
    let dayOrDays;
    if (valueIsEmpty.value) {
      if (pickerOptions.value.getDefaultValue) dayOrDays = pickerOptions.value.getDefaultValue();
    } else if (shared_esmBundler.isArray(props.modelValue)) dayOrDays = props.modelValue.map((d) => utils.parseDate(d, props.valueFormat, lang.value));
    else dayOrDays = utils.parseDate(props.modelValue ?? "", props.valueFormat, lang.value);
    if (pickerOptions.value.getRangeAvailableTime) {
      const availableResult = pickerOptions.value.getRangeAvailableTime(dayOrDays);
      if (!isEqual.default(availableResult, dayOrDays)) {
        dayOrDays = availableResult;
        if (!valueIsEmpty.value) emitInput(utils.dayOrDaysToDate(dayOrDays));
      }
    }
    if (shared_esmBundler.isArray(dayOrDays) && dayOrDays.some((day) => !day)) dayOrDays = [];
    return dayOrDays;
  });
  const pickerOptions = vue.ref({});
  const onSetPickerOption = (e) => {
    pickerOptions.value[e[0]] = e[1];
    pickerOptions.value.panelReady = true;
  };
  const onCalendarChange = (e) => {
    emit("calendar-change", e);
  };
  const onPanelChange = (value, mode, view) => {
    emit("panel-change", value, mode, view);
  };
  const onPick = (date = "", visible = false) => {
    pickerVisible.value = visible;
    let result;
    if (shared_esmBundler.isArray(date)) result = date.map((_) => _.toDate());
    else result = date ? date.toDate() : date;
    userInput.value = null;
    emitInput(result);
  };
  return {
    parsedValue,
    pickerActualVisible,
    pickerOptions,
    pickerVisible,
    userInput,
    valueIsEmpty,
    emitInput,
    onCalendarChange,
    onPanelChange,
    onPick,
    onSetPickerOption
  };
};
exports.useCommonPicker = useCommonPicker;
