import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { checkIfAllValuesAreEqual, createTranslationText, locales as locales$1 } from "./utils.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { isString } from "@pisell/utils";
import { Form, Input, Modal } from "antd";
import { GlobalOutlined } from "@ant-design/icons";
import classNames from "classnames";
import "./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 = _objectWithoutProperties(props, _excluded);
	const context = useEngineContext();
	const [open, setOpen] = useState(false);
	const [_value, set_value] = 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 = useCallback(() => {
		console.log(type, props, "翻译组件type值11");
		switch (type) {
			case "textarea": return Input.TextArea;
			default: return Input;
		}
	}, [type])();
	useEffect(() => {
		let _newValue = _objectSpread2({}, value);
		if (isString(value)) _newValue = createTranslationText(value);
		set_value(_objectSpread2(_objectSpread2({}, defaultValue), _newValue));
	}, [value]);
	const currentLocale = useMemo(() => {
		return shopLanguages.primary;
	}, [shopLanguages.primary]);
	const inputValue = useMemo(() => {
		if (isString(value)) return value;
		return value[currentLocale];
	}, [value, currentLocale]);
	const languageList = useMemo(() => {
		return shopLanguages.list;
	}, [shopLanguages.list]);
	const defaultValue = useMemo(() => {
		let obj = {};
		for (let item in locales$1) obj[item] = "";
		return obj;
	}, []);
	const allChange = (e) => {
		let newValue = _objectSpread2(_objectSpread2({}, defaultValue), value || {});
		if (checkIfAllValuesAreEqual(newValue)) for (let key in newValue) newValue[key] = e || "";
		else newValue[currentLocale] = e || "";
		onChange === null || onChange === void 0 || onChange(newValue);
	};
	const isActive = 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 = useMemo(() => {
		return /* @__PURE__ */ React.createElement(GlobalOutlined, {
			className: classNames("pisell-lowcode-translation-icon", { active: isActive }),
			onClick: () => setOpen(true)
		});
	}, [isActive]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, type === "textarea" ? /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-translation-textarea-wrapper" }, /* @__PURE__ */ React.createElement(InputComponent, _objectSpread2(_objectSpread2({}, others), {}, {
		value: inputValue,
		onChange: (e) => allChange(e.target.value)
	})), suffixIcon) : /* @__PURE__ */ React.createElement(InputComponent, _objectSpread2(_objectSpread2({}, others), {}, {
		value: inputValue,
		onChange: (e) => allChange(e.target.value),
		suffix: suffixIcon
	})), /* @__PURE__ */ React.createElement(Modal, {
		open,
		onOk: _onOk,
		onCancel: () => {
			set_value(value || defaultValue);
			setOpen(false);
		}
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-translation-modal" }, languageList.map((d) => {
		return /* @__PURE__ */ React.createElement(Form.Item, {
			label: d.text,
			labelCol: { span: 24 },
			key: d.code
		}, /* @__PURE__ */ React.createElement(InputComponent, _objectSpread2(_objectSpread2({}, typeof others.maxLength === "number" ? { maxLength: others.maxLength } : {}), {}, {
			value: _value[d.code],
			onChange: (e) => {
				set_value(_objectSpread2(_objectSpread2({}, _value), {}, { [d.code]: e.target.value }));
			},
			autoSize: type === "textarea" ? others === null || others === void 0 ? void 0 : others.autoSize : void 0
		})));
	}))));
};
//#endregion
export { Translation as default };
