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/systemSettings/registry/components/OptionDataSource/index.tsx
var OptionDataSource_exports = {};
__export(OptionDataSource_exports, {
  default: () => OptionDataSource_default
});
module.exports = __toCommonJS(OptionDataSource_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_OptionTable = __toESM(require("./OptionTable"));
var import_DataSourceTreeSelect = __toESM(require("../DataSourceTreeSelect"));
var import_index = require("./index.less");
var { TextArea } = import_antd.Input;
var optionSourceTypeOptions = [
  {
    title: "registry.optionDataSource.type.default-label",
    description: "registry.optionDataSource.type.default-desc",
    value: "default"
  },
  {
    title: "registry.optionDataSource.type.api-label",
    description: "registry.optionDataSource.type.api-desc",
    value: "api"
  },
  {
    title: "registry.optionDataSource.type.custom-label",
    description: "registry.optionDataSource.type.custom-desc",
    value: "custom"
  }
];
var OptionDataSource = ({
  form,
  treeDataSource
}) => {
  const type = import_antd.Form.useWatch(["xDataSource", "type"], form);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["xDataSource", "type"],
      label: import_utils.locales.getText("registry.optionDataSource.type.title"),
      rules: [
        {
          required: true,
          message: import_utils.locales.getText("registry.optionDataSource.type.desc")
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Group, { style: { width: "100%" } }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-settings-registry-option-source-type-options-desc" }, import_utils.locales.getText("registry.optionDataSource.type.desc")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-settings-registry-option-source-type-options" }, optionSourceTypeOptions.map((option) => /* @__PURE__ */ import_react.default.createElement(import_antd.Radio, { key: option.value, value: option.value }, /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "inline-block" } }, /* @__PURE__ */ import_react.default.createElement("div", null, import_utils.locales.getText(option.title)), /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          fontSize: "12px",
          color: "#8c8c8c",
          marginTop: "2px"
        }
      },
      import_utils.locales.getText(option.description)
    ))))))
  ), type === "default" && /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["xDataSource", "options"],
      label: import_utils.locales.getText("registry.optionDataSource.options.label"),
      validateTrigger: ["onSubmit", "onChange"],
      rules: [
        {
          required: true,
          validator: (_, value) => {
            if (!value || !Array.isArray(value) || value.length === 0) {
              return Promise.reject(
                new Error(
                  import_utils.locales.getText("registry.optionDataSource.options.empty")
                )
              );
            }
            for (let i = 0; i < value.length; i++) {
              const item = value[i];
              if (!item.label || !item.value || !item.value.trim()) {
                return Promise.reject(
                  new Error(
                    import_utils.locales.getText(
                      "registry.optionDataSource.options.required-desc"
                    )
                  )
                );
              }
            }
            return Promise.resolve();
          }
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(import_OptionTable.default, null)
  ), type === "api" && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["xDataSource", "dataSource"],
      label: import_utils.locales.getText(
        "registry.optionDataSource.api.dataSource-label"
      ),
      rules: [
        {
          required: true,
          validator: (_, value) => {
            if (!value || !(value == null ? void 0 : value.key) && !(value == null ? void 0 : value.tableName)) {
              return Promise.reject(
                new Error(
                  import_utils.locales.getText(
                    "registry.optionDataSource.api.dataSource-required"
                  )
                )
              );
            }
            return Promise.resolve();
          }
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_DataSourceTreeSelect.default,
      {
        mode: "list",
        filters: {
          tables: "all",
          variables: {}
        },
        dataSource: treeDataSource
      }
    )
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["xDataSource", "extraParams"],
      label: import_utils.locales.getText(
        "registry.optionDataSource.api.extraParams-label"
      ),
      rules: [
        {
          validator: (_, value) => {
            if (!value || value.trim() === "") {
              return Promise.resolve();
            }
            try {
              JSON.parse(value);
              return Promise.resolve();
            } catch (error) {
              return Promise.reject(
                new Error(
                  import_utils.locales.getText("registry.message.uiSchema.invalid")
                )
              );
            }
          }
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(
      TextArea,
      {
        placeholder: import_utils.locales.getText(
          "registry.optionDataSource.api.extraParams-label"
        ),
        rows: 6,
        style: { fontFamily: "monospace" }
      }
    )
  )), type === "custom" && /* @__PURE__ */ import_react.default.createElement(
    import_antd.Alert,
    {
      type: "warning",
      showIcon: true,
      message: import_utils.locales.getText(
        "registry.optionDataSource.type.custom.warning-title"
      ),
      description: import_utils.locales.getText(
        "registry.optionDataSource.type.custom.warning-description"
      ),
      className: "pisell-settings-registry-option-custom-alert"
    }
  ));
};
var OptionDataSource_default = OptionDataSource;
