const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_OptionTable = require("./OptionTable.js");
const require_index = require("../DataSourceTreeSelect/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/systemSettings/registry/components/OptionDataSource/index.tsx
const { TextArea } = antd.Input;
const 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"
	}
];
const OptionDataSource = ({ form, treeDataSource }) => {
	const type = antd.Form.useWatch(["xDataSource", "type"], form);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["xDataSource", "type"],
		label: _pisell_utils.locales.getText("registry.optionDataSource.type.title"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("registry.optionDataSource.type.desc")
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.Radio.Group, { style: { width: "100%" } }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-settings-registry-option-source-type-options-desc" }, _pisell_utils.locales.getText("registry.optionDataSource.type.desc")), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-settings-registry-option-source-type-options" }, optionSourceTypeOptions.map((option) => /* @__PURE__ */ react.default.createElement(antd.Radio, {
		key: option.value,
		value: option.value
	}, /* @__PURE__ */ react.default.createElement("div", { style: { display: "inline-block" } }, /* @__PURE__ */ react.default.createElement("div", null, _pisell_utils.locales.getText(option.title)), /* @__PURE__ */ react.default.createElement("div", { style: {
		fontSize: "12px",
		color: "#8c8c8c",
		marginTop: "2px"
	} }, _pisell_utils.locales.getText(option.description)))))))), type === "default" && /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["xDataSource", "options"],
		label: _pisell_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(_pisell_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(_pisell_utils.locales.getText("registry.optionDataSource.options.required-desc")));
				}
				return Promise.resolve();
			}
		}]
	}, /* @__PURE__ */ react.default.createElement(require_OptionTable.default, null)), type === "api" && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["xDataSource", "dataSource"],
		label: _pisell_utils.locales.getText("registry.optionDataSource.api.dataSource-label"),
		rules: [{
			required: true,
			validator: (_, value) => {
				if (!value || !(value === null || value === void 0 ? void 0 : value.key) && !(value === null || value === void 0 ? void 0 : value.tableName)) return Promise.reject(new Error(_pisell_utils.locales.getText("registry.optionDataSource.api.dataSource-required")));
				return Promise.resolve();
			}
		}]
	}, /* @__PURE__ */ react.default.createElement(require_index.default, {
		mode: "list",
		filters: {
			tables: "all",
			variables: {}
		},
		dataSource: treeDataSource
	})), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["xDataSource", "extraParams"],
		label: _pisell_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(_pisell_utils.locales.getText("registry.message.uiSchema.invalid")));
			}
		} }]
	}, /* @__PURE__ */ react.default.createElement(TextArea, {
		placeholder: _pisell_utils.locales.getText("registry.optionDataSource.api.extraParams-label"),
		rows: 6,
		style: { fontFamily: "monospace" }
	}))), type === "custom" && /* @__PURE__ */ react.default.createElement(antd.Alert, {
		type: "warning",
		showIcon: true,
		message: _pisell_utils.locales.getText("registry.optionDataSource.type.custom.warning-title"),
		description: _pisell_utils.locales.getText("registry.optionDataSource.type.custom.warning-description"),
		className: "pisell-settings-registry-option-custom-alert"
	}));
};
//#endregion
exports.default = OptionDataSource;
