var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellDatePicker/datePickerCpt.tsx
var datePickerCpt_exports = {};
__export(datePickerCpt_exports, {
  default: () => datePickerCpt_default
});
module.exports = __toCommonJS(datePickerCpt_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_detePickerCpt = require("./detePickerCpt.less");
var isValidDate = (dateString, format) => {
  const date = window.dayjs(dateString, format, true);
  return date.isValid();
};
var DatePickerInput = (props) => {
  const { format, value, onChange, onDateChange, ...others } = props;
  const eventChange = (val) => {
    onChange == null ? void 0 : onChange({ target: { value: val } });
    if (isValidDate(val, format)) {
      onDateChange == null ? void 0 : onDateChange(
        window.dayjs(val, format),
        window.dayjs(val, format).format(format)
      );
    }
  };
  const handleChange = (e) => {
    let inputValue = e.target.value;
    if (!/^[\d/]*$/.test(inputValue)) {
      return;
    }
    let parts = format.split(/[^a-zA-Z]/);
    let separators = format.match(/[^a-zA-Z]+/g) || [];
    if (inputValue.length < value.length) {
      eventChange == null ? void 0 : eventChange(inputValue);
      return;
    }
    let cursor = 0;
    let result = "";
    let lastIndex = 0;
    for (let i = 0; i < parts.length; i++) {
      const part = parts[i];
      if (!part)
        continue;
      const separator = separators[i] || "";
      const expectedLength = part.length;
      lastIndex = lastIndex + part.length + separator.length;
      let start = cursor;
      if (i > 0) {
        start += separators.slice(0, i).join("").length;
      }
      let end = start + expectedLength;
      let userPart = inputValue.slice(start, end);
      if (userPart.length > 0) {
        result += separators[i - 1] || "";
      }
      if (userPart.length === 0 && result.slice(result.length - 1) !== inputValue.slice(result.length - 1)) {
        userPart = inputValue.slice(result.length - 1);
      }
      if (part.includes("Y")) {
        result += userPart;
      } else {
        if (userPart.length === 1 && ["M", "D", "H", "m", "s"].includes(part[0])) {
          if ("M" === part[0] && userPart > 1) {
            userPart = "0" + userPart;
          }
          if ("D" === part[0] && userPart > 3) {
            userPart = "0" + userPart;
          }
          if ("H" === part[0] && userPart > 2) {
            userPart = "0" + userPart;
          }
          if ("m" === part[0] && userPart > 7) {
            userPart = "0" + userPart;
          }
          if ("s" === part[0] && userPart > 7) {
            userPart = "0" + userPart;
          }
        }
        result += userPart;
      }
      if (userPart.length === expectedLength && !inputValue.slice(lastIndex - 1).startsWith(separator)) {
        result += separator;
      }
      cursor += userPart.length;
    }
    eventChange == null ? void 0 : eventChange(result);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Input, { ...others, type: "tel", value, onChange: handleChange });
};
var DatePickerCpt = (props) => {
  const { format, type, onChange, size, picker } = props;
  const propsObj = (0, import_react.useMemo)(() => {
    if (picker && picker !== "date") {
      return {
        inputRender: void 0
      };
    }
    if (type === "input") {
      return {
        popupClassName: "pisell-date-picker-type-input",
        suffixIcon: null,
        placeholder: format || ""
      };
    } else if (type === "select") {
      return {
        inputReadOnly: true
      };
    }
    return {};
  }, [type, picker, format]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.DatePicker,
    {
      ...props,
      className: "pisell-date-picker",
      inputRender: (props2) => {
        return /* @__PURE__ */ import_react.default.createElement(
          DatePickerInput,
          {
            ...props2,
            onDateChange: onChange,
            format
          }
        );
      },
      ...propsObj
    }
  );
};
var datePickerCpt_default = DatePickerCpt;
