const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/translation/index.tsx
const _excluded = [
	"value",
	"onChange",
	"type"
];
const defaultGlobal = {
	shopLanguages: {
		primary: "en",
		list: [
			{
				id: 1,
				code: "zh-CN",
				text: "简体中文",
				primary: 1,
				status: "published"
			},
			{
				id: 2,
				code: "en",
				text: "English",
				primary: 0,
				status: "published"
			},
			{
				id: 3,
				code: "zh-HK",
				text: "繁體中文",
				primary: 0,
				status: "published"
			},
			{
				id: 4,
				code: "ja",
				text: "日本語",
				primary: 0,
				status: "published"
			},
			{
				id: 5,
				code: "pt",
				text: "Português",
				primary: 0,
				status: "published"
			}
		]
	},
	primary_locale: "en"
};
const defaultObj = {};
const Translation = (props) => {
	var _context$appHelper, _store$getState;
	const { value = defaultObj, onChange, type = "text" } = props, others = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const context = require_useEngineContext.default();
	const [open, setOpen] = (0, react.useState)(false);
	const [_value, set_value] = (0, react.useState)(value);
	const store = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.store;
	const { shopLanguages } = (store === null || store === void 0 || (_store$getState = store.getState()) === null || _store$getState === void 0 ? void 0 : _store$getState.global) || defaultGlobal;
	const InputComponent = (0, react.useCallback)(() => {
		console.log(type, props, "翻译组件type值11");
		switch (type) {
			case "textarea": return antd.Input.TextArea;
			default: return antd.Input;
		}
	}, [type])();
	(0, react.useEffect)(() => {
		let _newValue = require_objectSpread2._objectSpread2({}, value);
		if ((0, _pisell_utils.isString)(value)) _newValue = require_utils.createTranslationText(value);
		set_value(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaultValue), _newValue));
	}, [value]);
	const currentLocale = (0, react.useMemo)(() => {
		return shopLanguages.primary;
	}, [shopLanguages.primary]);
	const inputValue = (0, react.useMemo)(() => {
		if ((0, _pisell_utils.isString)(value)) return value;
		return value[currentLocale];
	}, [value, currentLocale]);
	const languageList = (0, react.useMemo)(() => {
		return shopLanguages.list;
	}, [shopLanguages.list]);
	const defaultValue = (0, react.useMemo)(() => {
		let obj = {};
		for (let item in require_utils.locales) obj[item] = "";
		return obj;
	}, []);
	const allChange = (e) => {
		let newValue = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaultValue), value || {});
		if (require_utils.checkIfAllValuesAreEqual(newValue)) for (let key in newValue) newValue[key] = e || "";
		else newValue[currentLocale] = e || "";
		onChange === null || onChange === void 0 || onChange(newValue);
	};
	const isActive = (0, react.useMemo)(() => {
		let bol = false;
		for (let key in value) if (key !== currentLocale && value[key]) bol = true;
		return bol;
	}, [value, currentLocale]);
	const _onOk = () => {
		onChange === null || onChange === void 0 || onChange(_value);
		setOpen(false);
	};
	/**
	* @title: 多语言图标
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-08-13 14:39
	*/
	const suffixIcon = (0, react.useMemo)(() => {
		return /* @__PURE__ */ react.default.createElement(_ant_design_icons.GlobalOutlined, {
			className: (0, classnames.default)("pisell-lowcode-translation-icon", { active: isActive }),
			onClick: () => setOpen(true)
		});
	}, [isActive]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, type === "textarea" ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode-translation-textarea-wrapper" }, /* @__PURE__ */ react.default.createElement(InputComponent, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, others), {}, {
		value: inputValue,
		onChange: (e) => allChange(e.target.value)
	})), suffixIcon) : /* @__PURE__ */ react.default.createElement(InputComponent, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, others), {}, {
		value: inputValue,
		onChange: (e) => allChange(e.target.value),
		suffix: suffixIcon
	})), /* @__PURE__ */ react.default.createElement(antd.Modal, {
		open,
		onOk: _onOk,
		onCancel: () => {
			set_value(value || defaultValue);
			setOpen(false);
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode-translation-modal" }, languageList.map((d) => {
		return /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: d.text,
			labelCol: { span: 24 },
			key: d.code
		}, /* @__PURE__ */ react.default.createElement(InputComponent, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, typeof others.maxLength === "number" ? { maxLength: others.maxLength } : {}), {}, {
			value: _value[d.code],
			onChange: (e) => {
				set_value(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _value), {}, { [d.code]: e.target.value }));
			},
			autoSize: type === "textarea" ? others === null || others === void 0 ? void 0 : others.autoSize : void 0
		})));
	}))));
};
//#endregion
exports.default = Translation;
