var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_picker = __toESM(require("../picker"));
var import_utils = require("./utils");
var import_hooks = require("../hooks");
var import_date = require("../utils/validate/date");
var import_utils2 = require("../utils");
var import_raf = require("../utils/raf");
var import_use_ref_state = __toESM(require("../hooks/use-ref-state"));
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
const DatePicker = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const _a = props, {
    value,
    formatter,
    columnsOrder,
    type: datePickerType,
    filter,
    minDate: datePickerMinDate,
    maxDate: datePickerMaxDate
  } = _a, pickerProps = __objRest(_a, [
    "value",
    "formatter",
    "columnsOrder",
    "type",
    "filter",
    "minDate",
    "maxDate"
  ]);
  const formatValue = (date) => {
    if (!(0, import_date.isDate)(date)) {
      date = props.minDate;
    }
    date = Math.max(new Date(date).getTime(), props.minDate.getTime());
    date = Math.min(date, props.maxDate.getTime());
    return new Date(date);
  };
  const pickerRef = (0, import_react.useRef)(null);
  const [currentDate, setCurrentDate, currentDateRef] = (0, import_use_ref_state.default)(formatValue(value));
  const getBoundary = (type, dateValue) => {
    const boundary = props[`${type}Date`];
    const year = boundary.getFullYear();
    let month = 1;
    let date = 1;
    let hour = 0;
    let minute = 0;
    if (type === "max") {
      month = 12;
      date = (0, import_utils.getMonthEndDay)(dateValue.getFullYear(), dateValue.getMonth() + 1);
      hour = 23;
      minute = 59;
    }
    if (dateValue.getFullYear() === year) {
      month = boundary.getMonth() + 1;
      if (dateValue.getMonth() + 1 === month) {
        date = boundary.getDate();
        if (dateValue.getDate() === date) {
          hour = boundary.getHours();
          if (dateValue.getHours() === hour) {
            minute = boundary.getMinutes();
          }
        }
      }
    }
    return {
      [`${type}Year`]: year,
      [`${type}Month`]: month,
      [`${type}Date`]: date,
      [`${type}Hour`]: hour,
      [`${type}Minute`]: minute
    };
  };
  const ranges = (0, import_react.useMemo)(() => {
    const {
      maxYear,
      maxDate: maxDate2,
      maxMonth,
      maxHour,
      maxMinute
    } = getBoundary("max", currentDateRef.current);
    const {
      minYear,
      minDate: minDate2,
      minMonth,
      minHour,
      minMinute
    } = getBoundary("min", currentDateRef.current);
    let result = [{
      type: "year",
      range: [minYear, maxYear]
    }, {
      type: "month",
      range: [minMonth, maxMonth]
    }, {
      type: "day",
      range: [minDate2, maxDate2]
    }, {
      type: "hour",
      range: [minHour, maxHour]
    }, {
      type: "minute",
      range: [minMinute, maxMinute]
    }];
    switch (props.type) {
      case "date":
        result = result.slice(0, 3);
        break;
      case "year-month":
        result = result.slice(0, 2);
        break;
      case "month-day":
        result = result.slice(1, 3);
        break;
      case "datehour":
        result = result.slice(0, 4);
        break;
      default:
        break;
    }
    if (columnsOrder) {
      const columnsOrderArr = columnsOrder.concat(result.map((column) => column.type));
      result.sort((a, b) => columnsOrderArr.indexOf(a.type) - columnsOrderArr.indexOf(b.type));
    }
    return result;
  }, [columnsOrder, props.type, currentDateRef.current]);
  const originColumns = (0, import_react.useMemo)(() => ranges.map(({
    type,
    range: rangeArr
  }) => {
    let values = (0, import_utils.times)(rangeArr[1] - rangeArr[0] + 1, (index) => {
      return (0, import_utils2.padZero)(rangeArr[0] + index);
    });
    if (filter) {
      values = filter(type, values);
    }
    return {
      type,
      values
    };
  }), [filter, ranges]);
  const columns = (0, import_react.useMemo)(() => {
    return originColumns.map((column) => ({
      values: column.values.map((e) => formatter(column.type, e))
    }));
  }, [originColumns, formatter]);
  const updateColumnValue = (0, import_react.useCallback)(() => {
    const values = originColumns.map((column) => {
      switch (column.type) {
        case "year":
          return formatter("year", `${currentDate.getFullYear()}`);
        case "month":
          return formatter("month", (0, import_utils2.padZero)(currentDate.getMonth() + 1));
        case "day":
          return formatter("day", (0, import_utils2.padZero)(currentDate.getDate()));
        case "hour":
          return formatter("hour", (0, import_utils2.padZero)(currentDate.getHours()));
        case "minute":
          return formatter("minute", (0, import_utils2.padZero)(currentDate.getMinutes()));
        default:
          return null;
      }
    });
    (0, import_raf.doubleRaf)(() => {
      var _a2;
      (_a2 = pickerRef.current) == null ? void 0 : _a2.setValues(values);
    });
  }, [currentDate, formatter, originColumns]);
  const updateInnerValue = (indexes) => {
    const {
      type
    } = props;
    const getValue = (datetimePickerColumnType) => {
      let index = 0;
      originColumns.forEach((column, columnIndex) => {
        if (datetimePickerColumnType === column.type) {
          index = columnIndex;
        }
      });
      const {
        values
      } = originColumns[index];
      return (0, import_utils.getTrueValue)(values[indexes[index]]);
    };
    let year = null;
    let month = null;
    let day = null;
    if (type === "month-day") {
      year = (currentDate || props.minDate).getFullYear();
      month = getValue("month");
      day = getValue("day");
    } else {
      year = getValue("year");
      month = getValue("month");
      day = type === "year-month" ? 1 : getValue("day");
    }
    const maxDay = (0, import_utils.getMonthEndDay)(year, month);
    day = day > maxDay ? maxDay : day;
    let hour = 0;
    let minute = 0;
    if (type === "datehour") {
      hour = +getValue("hour");
    }
    if (type === "datetime") {
      hour = +getValue("hour");
      minute = +getValue("minute");
    }
    return formatValue(new Date(year, month - 1, day, hour, minute));
  };
  const onChange = () => {
    var _a2;
    if (pickerRef.current) {
      const indexes = pickerRef.current.getIndexes();
      const nextValue = updateInnerValue(indexes);
      setCurrentDate(nextValue);
      (_a2 = props.onChange) == null ? void 0 : _a2.call(props, nextValue);
    }
  };
  const onConfirm = () => {
    var _a2;
    (_a2 = props.onConfirm) == null ? void 0 : _a2.call(props, currentDate);
  };
  (0, import_react.useEffect)(() => {
    updateColumnValue();
  }, [updateColumnValue]);
  const {
    minDate,
    maxDate
  } = props;
  (0, import_hooks.useUpdateEffect)(() => {
    const nextValue = formatValue(value);
    if (nextValue && nextValue.valueOf() !== (currentDate == null ? void 0 : currentDate.valueOf())) {
      setCurrentDate(nextValue);
    }
  }, [value, filter, minDate, maxDate]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    getPicker: () => pickerRef.current
  }));
  return /* @__PURE__ */ import_react.default.createElement(import_picker.default, _extends({}, pickerProps, {
    ref: pickerRef,
    columns,
    onChange,
    onConfirm,
    onCancel: props.onCancel
  }));
});
const currentYear = new Date().getFullYear();
DatePicker.defaultProps = {
  type: "datetime",
  minDate: new Date(currentYear - 10, 0, 1),
  maxDate: new Date(currentYear + 10, 11, 31),
  formatter: (type, value) => value
};
DatePicker.displayName = "DatePicker";
var stdin_default = /* @__PURE__ */ (0, import_react.memo)(DatePicker);
