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/dataSourceComponents/fields/RecordListWrapper/WithMode.tsx
var WithMode_exports = {};
__export(WithMode_exports, {
  default: () => WithMode_default
});
module.exports = __toCommonJS(WithMode_exports);
var import_react = __toESM(require("react"));
var import_utils = require("../../dataSourceForm/utils");
var import_ReadPretty = __toESM(require("./ReadPretty"));
var import_Tabs = __toESM(require("../Tabs/Tabs"));
var import_dataSourceSubForm = __toESM(require("../../dataSourceSubForm"));
var RecordListWrapperBase = (props) => {
  const {
    value,
    onChange,
    options = [],
    children,
    xRecordSchema,
    topSlot,
    slot,
    renderMode,
    __designMode,
    defaultValue
  } = props;
  const isMounted = (0, import_react.useRef)(false);
  const dataSourceSubFormRef = (0, import_react.useRef)(null);
  const safeValue = (0, import_react.useMemo)(() => {
    if (value && typeof value === "object" && !Array.isArray(value)) {
      return value;
    }
    return {};
  }, [value]);
  const [activeKey, setActiveKey] = (0, import_react.useState)(() => {
    var _a;
    if (defaultValue != null) return String(defaultValue);
    return String(((_a = options[0]) == null ? void 0 : _a.value) || "");
  });
  (0, import_react.useEffect)(() => {
    if (defaultValue != null) {
      setActiveKey(String(defaultValue));
    }
  }, [defaultValue]);
  (0, import_react.useEffect)(() => {
    var _a;
    if (options.length && !options.find((o) => String(o.value) === activeKey)) {
      setActiveKey(
        defaultValue != null ? String(defaultValue) : String(((_a = options[0]) == null ? void 0 : _a.value) || "")
      );
    }
  }, [options]);
  (0, import_react.useEffect)(() => {
    var _a, _b;
    if (isMounted.current) {
      return;
    }
    if ((options == null ? void 0 : options.length) > 0 && dataSourceSubFormRef.current) {
      const defaultValues = ((_b = (_a = dataSourceSubFormRef.current).getFieldsValue) == null ? void 0 : _b.call(_a)) ?? {};
      const completeValue = options.reduce(
        (acc, option) => {
          if (option.value === null || option.value === void 0) return acc;
          acc[String(option.value)] = {
            ...defaultValues,
            ...safeValue[String(option.value)] || {}
          };
          return acc;
        },
        {}
      );
      onChange == null ? void 0 : onChange(completeValue);
      isMounted.current = true;
    }
  }, [options, safeValue]);
  const renderTopSlot = () => {
    const slotElement = topSlot || slot;
    if (slotElement && import_react.default.isValidElement(slotElement)) {
      return import_react.default.cloneElement(slotElement, {
        options,
        value: activeKey,
        onChange: setActiveKey
      });
    }
    const tabOptions = options.map((o) => ({
      ...o,
      label: o.label ?? String(o.value)
    }));
    if (!options.length) {
      return /* @__PURE__ */ import_react.default.createElement("div", { style: { color: "#999", fontSize: 16 } }, "暂无选项，请配置数据源。");
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_Tabs.default,
      {
        options: tabOptions,
        value: activeKey,
        onChange: (key) => setActiveKey(String(key)),
        disabled: renderMode === "disabled"
      }
    );
  };
  const renderBottom = () => {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
      import_dataSourceSubForm.default,
      {
        activeKey,
        value: safeValue[activeKey],
        onChange: (recordValue) => onChange == null ? void 0 : onChange({ ...safeValue || {}, [activeKey]: recordValue || {} }),
        xRecordSchema,
        renderMode,
        __designMode
      },
      children
    ), /* @__PURE__ */ import_react.default.createElement("div", { style: { width: "100%", height: 0, visibility: "hidden", overflow: "hidden" } }, /* @__PURE__ */ import_react.default.createElement(
      import_dataSourceSubForm.default,
      {
        key: "record-list-wrapper-default-value-form",
        ref: dataSourceSubFormRef,
        xRecordSchema,
        renderMode,
        __designMode
      },
      children
    )));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { style: { marginBottom: 12 } }, renderTopSlot()), renderBottom());
};
var RecordListWrapperWithMode = (0, import_utils.withMode)(
  RecordListWrapperBase,
  import_ReadPretty.default
);
var WithMode_default = RecordListWrapperWithMode;
