import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import { DEVICE_STATUSES, validateI18nRequired } from "../../types.js";
import React, { forwardRef, useEffect, useImperativeHandle, useMemo } from "react";
import { Form, Select } from "antd";
import { locales } from "@pisell/utils";
import { BaseTranslation } from "@pisell/materials";
//#region src/plus/saasDevice/deviceProfile/components/ProfileSetting/BasicForm.tsx
const getText = locales.getText;
const EMPTY_I18N = {
	original: "",
	"zh-CN": "",
	"zh-HK": "",
	en: "",
	pt: "",
	ja: ""
};
const BasicForm = forwardRef(({ mode, deviceType, deviceModels, initialValue, onDirty, open = true }, ref) => {
	var _context$appHelper;
	const context = useEngineContext();
	const [form] = Form.useForm();
	const { translation } = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils;
	const initialValues = useMemo(() => {
		if (mode === "edit" && initialValue) {
			var _initialValue$_raw$de, _initialValue$_raw;
			return {
				name: typeof (initialValue === null || initialValue === void 0 ? void 0 : initialValue.name) === "string" ? _objectSpread2(_objectSpread2({}, EMPTY_I18N), {}, { original: initialValue === null || initialValue === void 0 ? void 0 : initialValue.name }) : initialValue === null || initialValue === void 0 ? void 0 : initialValue.name,
				type_code: (deviceType === null || deviceType === void 0 ? void 0 : deviceType.code) || "",
				model_code: (initialValue === null || initialValue === void 0 ? void 0 : initialValue.modelCode) || (initialValue === null || initialValue === void 0 ? void 0 : initialValue.model_code),
				description: (_initialValue$_raw$de = initialValue === null || initialValue === void 0 || (_initialValue$_raw = initialValue._raw) === null || _initialValue$_raw === void 0 ? void 0 : _initialValue$_raw.description) !== null && _initialValue$_raw$de !== void 0 ? _initialValue$_raw$de : _objectSpread2({}, EMPTY_I18N),
				status: initialValue === null || initialValue === void 0 ? void 0 : initialValue.status
			};
		}
		return {
			name: _objectSpread2({}, EMPTY_I18N),
			type_code: (deviceType === null || deviceType === void 0 ? void 0 : deviceType.code) || "",
			model_code: "",
			description: _objectSpread2({}, EMPTY_I18N),
			status: "active"
		};
	}, [
		mode,
		initialValue,
		deviceType === null || deviceType === void 0 ? void 0 : deviceType.code
	]);
	useEffect(() => {
		if (!open) return;
		form.resetFields();
		form.setFieldsValue(initialValues);
	}, [
		open,
		initialValues,
		form
	]);
	useImperativeHandle(ref, () => ({
		getValues: () => form.validateFields(),
		resetDirty: () => {}
	}), [form]);
	const typeOptions = useMemo(() => [{
		label: translation((deviceType === null || deviceType === void 0 ? void 0 : deviceType.name) || "") || (deviceType === null || deviceType === void 0 ? void 0 : deviceType.code) || "",
		value: (deviceType === null || deviceType === void 0 ? void 0 : deviceType.code) || ""
	}], [deviceType === null || deviceType === void 0 ? void 0 : deviceType.code, deviceType === null || deviceType === void 0 ? void 0 : deviceType.name]);
	const modelOptions = useMemo(() => deviceModels === null || deviceModels === void 0 ? void 0 : deviceModels.map((m) => ({
		label: translation(m.name) || m.code,
		value: m.code
	})), [deviceModels]);
	const statusOptions = useMemo(() => DEVICE_STATUSES.map((s) => ({
		label: getText(s.labelKey),
		value: s.value
	})), []);
	return /* @__PURE__ */ React.createElement(Form, {
		form,
		layout: "vertical",
		onValuesChange: () => onDirty === null || onDirty === void 0 ? void 0 : onDirty(),
		preserve: false,
		initialValues
	}, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "name",
		label: getText("pisell2.saas.device-profile.setting.field.name"),
		required: true,
		rules: [{ validator: validateI18nRequired(getText("pisell2.saas.device-profile.setting.field.nameRequired")) }]
	}, /* @__PURE__ */ React.createElement(BaseTranslation, {
		renderMode: "edit",
		placeholder: getText("pisell2.saas.device-profile.setting.field.namePlaceholder")
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "type_code",
		label: getText("pisell2.saas.device-profile.setting.field.deviceType"),
		required: true
	}, /* @__PURE__ */ React.createElement(Select, {
		disabled: true,
		options: typeOptions
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "model_code",
		label: getText("pisell2.saas.device-profile.setting.field.model"),
		required: true,
		rules: [{
			required: true,
			message: getText("pisell2.saas.device-profile.setting.field.modelRequired")
		}]
	}, /* @__PURE__ */ React.createElement(Select, {
		disabled: mode === "edit",
		placeholder: getText("pisell2.saas.device-profile.setting.field.modelPlaceholder"),
		options: modelOptions
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "status",
		label: getText("pisell2.saas.device-profile.setting.field.status"),
		required: true
	}, /* @__PURE__ */ React.createElement(Select, { options: statusOptions })), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "description",
		label: getText("pisell2.saas.device-profile.setting.field.description")
	}, /* @__PURE__ */ React.createElement(BaseTranslation, {
		renderMode: "edit",
		type: "textarea",
		placeholder: getText("pisell2.saas.device-profile.setting.field.descriptionPlaceholder")
	})));
});
BasicForm.displayName = "BasicForm";
//#endregion
export { BasicForm as default };
