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(
  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_classnames = __toESM(require("classnames"));
var import_PickerColumn = __toESM(require("./PickerColumn"));
var import_loading = require("../loading");
var import_use_refs = __toESM(require("../hooks/use-refs"));
var import_utils = require("../utils");
var import_constant = require("../utils/constant");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
const Picker = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("picker", prefixCls);
  const [refs, setRefs] = (0, import_use_refs.default)();
  const [formattedColumns, setFormattedColumns] = (0, import_react.useState)([]);
  const {
    text: textKey,
    values: valuesKey,
    children: childrenKey
  } = (0, import_utils.extend)({
    text: "text",
    values: "values",
    children: "children"
  }, props.columnsFieldNames);
  const wrapper = (0, import_react.useRef)(null);
  const itemHeight = (0, import_react.useMemo)(() => (0, import_utils.unitToPx)(props.itemHeight), [props.itemHeight]);
  const dataType = (0, import_react.useMemo)(() => {
    const {
      columns
    } = props;
    const firstColumn = columns[0] || {};
    if (typeof firstColumn === "object") {
      if (childrenKey in firstColumn) {
        return "cascade";
      }
      if (valuesKey in firstColumn) {
        return "object";
      }
    }
    return "plain";
  }, [props.columns]);
  const formatCascade = () => {
    var _a;
    const formatted = [];
    let cursor = {
      [childrenKey]: props.columns
    };
    while (cursor && cursor[childrenKey]) {
      const children = cursor[childrenKey];
      let defaultIndex = (_a = cursor.defaultIndex) != null ? _a : +props.defaultIndex;
      while (children[defaultIndex] && children[defaultIndex].disabled) {
        if (defaultIndex < children.length - 1) {
          defaultIndex++;
        } else {
          defaultIndex = 0;
          break;
        }
      }
      formatted.push({
        [valuesKey]: cursor[childrenKey],
        className: cursor.className,
        defaultIndex
      });
      cursor = children[defaultIndex];
    }
    setFormattedColumns(formatted);
  };
  const format = () => {
    const {
      columns
    } = props;
    if (dataType === "plain") {
      setFormattedColumns([{
        [valuesKey]: columns
      }]);
    } else if (dataType === "cascade") {
      formatCascade();
    } else {
      setFormattedColumns(columns);
    }
  };
  const getIndexes = () => refs.map((_ref) => _ref.getIndex());
  const setColumnValues = (index, options) => {
    const column = refs[index];
    if (column) {
      column.setOptions(options);
    }
  };
  const onCascadeChange = (columnIndex) => {
    let cursor = {
      [childrenKey]: props.columns
    };
    const indexes = getIndexes();
    for (let i = 0; i <= columnIndex; i += 1) {
      cursor = cursor[childrenKey][indexes[i]];
    }
    while (cursor && cursor[childrenKey]) {
      columnIndex += 1;
      setColumnValues(columnIndex, cursor[childrenKey]);
      cursor = cursor[childrenKey][cursor.defaultIndex || 0];
    }
  };
  const getChild = (index) => refs[index];
  const getColumnValue = (index) => {
    const column = getChild(index);
    return (column == null ? void 0 : column.getValue()) || void 0;
  };
  const setColumnValue = (index, value) => {
    const column = getChild(index);
    if (column) {
      column.setValue(value);
      if (dataType === "cascade") {
        onCascadeChange(index);
      }
    }
  };
  const getColumnIndex = (index) => {
    const column = getChild(index);
    return (column == null ? void 0 : column.getIndex()) || 0;
  };
  const setColumnIndex = (columnIndex, optionIndex) => {
    const column = getChild(columnIndex);
    if (column) {
      column.setIndex(optionIndex);
      if (dataType === "cascade") {
        onCascadeChange(columnIndex);
      }
    }
  };
  const getColumnValues = (index) => {
    const column = getChild(index);
    return column == null ? void 0 : column.getOptions();
  };
  const getValues = () => refs.map((_ref) => _ref.getValue());
  const setValues = (values) => {
    values.forEach((value, index) => {
      setColumnValue(index, value);
    });
  };
  const setIndexes = (indexes) => {
    indexes.forEach((optionIndex, columnIndex) => {
      setColumnIndex(columnIndex, optionIndex);
    });
  };
  const onChange = (columnIndex) => {
    if (dataType === "cascade") {
      onCascadeChange(columnIndex);
    }
    if (props.onChange) {
      if (dataType === "plain") {
        props.onChange(getColumnValue(0), getColumnIndex(0));
      } else {
        props.onChange(getValues(), columnIndex);
      }
    }
  };
  const confirm = () => {
    if (props.onConfirm) {
      if (dataType === "plain") {
        props.onConfirm(getColumnValue(0), getColumnIndex(0));
      } else {
        props.onConfirm(getValues(), getIndexes());
      }
    }
  };
  const cancel = () => {
    var _a, _b;
    if (dataType === "plain") {
      (_a = props.onCancel) == null ? void 0 : _a.call(props, getColumnValue(0), getColumnIndex(0));
    } else {
      (_b = props.onCancel) == null ? void 0 : _b.call(props, getValues(), getIndexes());
    }
  };
  const renderTitle = () => {
    if (props.title) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("title"), "rc-ellipsis")
      }, props.title);
    }
    return null;
  };
  const renderCancel = () => {
    const text = props.cancelButtonText || "\u53D6\u6D88";
    return /* @__PURE__ */ import_react.default.createElement("button", {
      type: "button",
      className: (0, import_classnames.default)(bem("cancel")),
      onClick: cancel
    }, text);
  };
  const renderConfirm = () => {
    const text = props.confirmButtonText || "\u786E\u8BA4";
    return /* @__PURE__ */ import_react.default.createElement("button", {
      type: "button",
      className: (0, import_classnames.default)(bem("confirm")),
      onClick: confirm
    }, text);
  };
  const renderToolbar = () => {
    return props.showToolbar ? /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("toolbar"))
    }, props.toolbar || /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderCancel(), renderTitle(), renderConfirm())) : null;
  };
  const renderColumnItems = () => formattedColumns.map((item, columnIndex) => {
    var _a;
    return /* @__PURE__ */ import_react.default.createElement(import_PickerColumn.default, {
      key: String(columnIndex),
      optionRender: props.optionRender,
      ref: setRefs(columnIndex),
      textKey,
      readonly: props.readonly,
      className: item.className,
      itemHeight,
      defaultIndex: (_a = item.defaultIndex) != null ? _a : +props.defaultIndex,
      swipeDuration: props.swipeDuration,
      visibleItemCount: props.visibleItemCount,
      initialOptions: item[valuesKey],
      onChange: () => {
        onChange(columnIndex);
      }
    });
  });
  const renderColumns = () => {
    const wrapHeight = itemHeight * props.visibleItemCount;
    const frameStyle = {
      height: `${itemHeight}px`
    };
    const columnsStyle = {
      height: `${wrapHeight}px`
    };
    const maskStyle = {
      backgroundSize: `100% ${(wrapHeight - itemHeight) / 2}px`
    };
    return /* @__PURE__ */ import_react.default.createElement("div", {
      ref: wrapper,
      className: (0, import_classnames.default)(bem("columns")),
      style: columnsStyle
    }, renderColumnItems(), /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("mask")),
      style: maskStyle
    }), /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(import_constant.BORDER_UNSET_TOP_BOTTOM, bem("frame")),
      style: frameStyle
    }));
  };
  (0, import_react.useEffect)(() => {
    format();
  }, [props.columns]);
  (0, import_use_event_listener.default)("touchmove", import_utils.preventDefault, {
    target: wrapper.current,
    depends: [wrapper.current]
  });
  (0, import_react.useImperativeHandle)(ref, () => ({
    confirm,
    getValues,
    setValues,
    getIndexes,
    setIndexes,
    getColumnIndex,
    setColumnIndex,
    getColumnValue,
    setColumnValue,
    getColumnValues,
    setColumnValues
  }));
  return /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem(), props.className)
  }, props.toolbarPosition === "top" ? renderToolbar() : null, props.loading ? /* @__PURE__ */ import_react.default.createElement(import_loading.Loading, {
    className: (0, import_classnames.default)(bem("loading"))
  }) : null, props.columnsTop, renderColumns(), props.columnsBottom, props.toolbarPosition === "bottom" ? renderToolbar() : null);
});
Picker.defaultProps = {
  itemHeight: 44,
  visibleItemCount: 5,
  swipeDuration: 1e3,
  defaultIndex: 0,
  showToolbar: true,
  toolbarPosition: "top"
};
Picker.displayName = "Picker";
var stdin_default = Picker;
