'use strict';

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

var vue = require('vue');
var index = require('../../select/index.cjs');
var index$1 = require('../../radio/index.cjs');
var FormItemContext = require('../../form/src/FormItemContext.cjs');

const YearSelectOffset = 10;
const YearSelectTotal = 20;
function YearSelect(props) {
  const {
    fullscreen,
    validRange,
    generateConfig,
    locale,
    prefixCls,
    value,
    onChange,
    divRef
  } = props;
  const year = generateConfig.getYear(value || generateConfig.getNow());
  let start = year - YearSelectOffset;
  let end = start + YearSelectTotal;
  if (validRange) {
    start = generateConfig.getYear(validRange[0]);
    end = generateConfig.getYear(validRange[1]) + 1;
  }
  const suffix = locale && locale.year === "\u5E74" ? "\u5E74" : "";
  const options = [];
  for (let index = start; index < end; index++)
    options.push({
      label: `${index}${suffix}`,
      value: index
    });
  return vue.createVNode(index.Select, {
    "size": fullscreen ? void 0 : "small",
    "options": options,
    "value": year,
    "class": `${prefixCls}-year-select`,
    "onChange": (numYear) => {
      let newDate = generateConfig.setYear(value, numYear);
      if (validRange) {
        const [startDate, endDate] = validRange;
        const newYear = generateConfig.getYear(newDate);
        const newMonth = generateConfig.getMonth(newDate);
        if (newYear === generateConfig.getYear(endDate) && newMonth > generateConfig.getMonth(endDate))
          newDate = generateConfig.setMonth(newDate, generateConfig.getMonth(endDate));
        if (newYear === generateConfig.getYear(startDate) && newMonth < generateConfig.getMonth(startDate))
          newDate = generateConfig.setMonth(newDate, generateConfig.getMonth(startDate));
      }
      onChange(newDate);
    },
    "getPopupContainer": () => divRef.value
  }, null);
}
YearSelect.inheritAttrs = false;
function MonthSelect(props) {
  const {
    prefixCls,
    fullscreen,
    validRange,
    value,
    generateConfig,
    locale,
    onChange,
    divRef
  } = props;
  const month = generateConfig.getMonth(value || generateConfig.getNow());
  let start = 0;
  let end = 11;
  if (validRange) {
    const [rangeStart, rangeEnd] = validRange;
    const currentYear = generateConfig.getYear(value);
    if (generateConfig.getYear(rangeEnd) === currentYear)
      end = generateConfig.getMonth(rangeEnd);
    if (generateConfig.getYear(rangeStart) === currentYear)
      start = generateConfig.getMonth(rangeStart);
  }
  const months = locale.shortMonths || generateConfig.locale.getShortMonths(locale.locale);
  const options = [];
  for (let index = start; index <= end; index += 1) {
    options.push({
      label: months[index],
      value: index
    });
  }
  return vue.createVNode(index.Select, {
    "size": fullscreen ? void 0 : "small",
    "class": `${prefixCls}-month-select`,
    "value": month,
    "options": options,
    "onChange": (newMonth) => {
      onChange(generateConfig.setMonth(value, newMonth));
    },
    "getPopupContainer": () => divRef.value
  }, null);
}
MonthSelect.inheritAttrs = false;
function ModeSwitch(props) {
  const {
    prefixCls,
    locale,
    mode,
    fullscreen,
    onModeChange
  } = props;
  return vue.createVNode(index$1.RadioGroup, {
    "onChange": ({
      target: {
        value
      }
    }) => {
      onModeChange(value);
    },
    "value": mode,
    "size": fullscreen ? void 0 : "small",
    "class": `${prefixCls}-mode-switch`
  }, {
    default: () => [vue.createVNode(index$1.RadioButton, {
      "value": "month"
    }, {
      default: () => [locale.month]
    }), vue.createVNode(index$1.RadioButton, {
      "value": "year"
    }, {
      default: () => [locale.year]
    })]
  });
}
ModeSwitch.inheritAttrs = false;
var CalendarHeader = /* @__PURE__ */ vue.defineComponent({
  name: "CalendarHeader",
  inheritAttrs: false,
  props: ["mode", "prefixCls", "value", "validRange", "generateConfig", "locale", "mode", "fullscreen"],
  setup(_props, {
    attrs
  }) {
    const divRef = vue.ref(null);
    const formItemInputContext = FormItemContext.FormItemInputContext.useInject();
    FormItemContext.FormItemInputContext.useProvide(formItemInputContext, {
      isFormItemInput: false
    });
    return () => {
      const props = {
        ..._props,
        ...attrs
      };
      const {
        prefixCls,
        fullscreen,
        mode,
        onChange,
        onModeChange
      } = props;
      const sharedProps = {
        ...props,
        fullscreen,
        divRef
      };
      return vue.createVNode("div", {
        "class": `${prefixCls}-header`,
        "ref": divRef
      }, [vue.createVNode(YearSelect, {
        ...sharedProps,
        "onChange": (v) => {
          onChange(v, "year");
        }
      }, null), mode === "month" && vue.createVNode(MonthSelect, {
        ...sharedProps,
        "onChange": (v) => {
          onChange(v, "month");
        }
      }, null), vue.createVNode(ModeSwitch, {
        ...sharedProps,
        "onModeChange": onModeChange
      }, null)]);
    };
  }
});

exports.default = CalendarHeader;
//# sourceMappingURL=Header.cjs.map
