const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_types = require("../../types.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/plus/saasDevice/appDevicePlanning/components/AddDeviceModal/index.tsx
const isProfileRequired = (model) => {
	var _model$data;
	return Boolean(model === null || model === void 0 || (_model$data = model.data) === null || _model$data === void 0 || (_model$data = _model$data.profile_structure) === null || _model$data === void 0 ? void 0 : _model$data.required);
};
const hasI18nValue = (value) => {
	if (!value) return false;
	return Object.values(value).some((item) => String(item || "").trim());
};
const AddDeviceModal = ({ open, variant = "desktop", model, fallbackModelCode, workAreaId, workAreas, submitting, getText, translation, onClose, onSubmit }) => {
	var _model$type, _model$type2;
	const [form] = antd.Form.useForm();
	const isPhoneVariant = variant === "phone";
	const nameValue = antd.Form.useWatch("name", form);
	const profileValue = antd.Form.useWatch("profile_id", form);
	const workAreaValue = antd.Form.useWatch("workarea_id", form);
	const modelCode = (model === null || model === void 0 ? void 0 : model.code) || fallbackModelCode;
	const profileRequired = isProfileRequired(model);
	const profiles = (model === null || model === void 0 ? void 0 : model.active_profiles) || [];
	const effectiveWorkAreaId = workAreaId || workAreaValue;
	const canSave = Boolean(modelCode && effectiveWorkAreaId && hasI18nValue(nameValue) && (!profileRequired || profiles.length > 0) && (!profileRequired || Boolean(profileValue)));
	(0, react.useEffect)(() => {
		if (!open) return;
		form.resetFields();
		form.setFieldsValue({
			name: require_objectSpread2._objectSpread2({}, require_types.EMPTY_I18N),
			profile_id: void 0,
			workarea_id: workAreaId
		});
	}, [
		form,
		open,
		workAreaId
	]);
	const handleSave = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (!canSave || !modelCode || !effectiveWorkAreaId) return;
			const values = yield form.validateFields();
			onSubmit({
				workarea_id: effectiveWorkAreaId,
				model_code: modelCode,
				profile_id: profileRequired ? values.profile_id || null : null,
				name: values.name
			});
		});
		return function handleSave() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open,
		onCancel: onClose,
		title: getText("pisell2.saas.device-planning.add.configure-title"),
		footer: /* @__PURE__ */ react.default.createElement("div", {
			className: "add-device-modal__footer",
			style: { marginTop: 0 }
		}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "default",
			block: true,
			size: "large",
			onClick: onClose
		}, getText("pisell2.saas.device-planning.add.cancel")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			block: true,
			size: "large",
			disabled: !canSave,
			loading: submitting,
			onClick: handleSave
		}, getText("pisell2.saas.device-planning.add.save"))),
		destroyOnClose: true,
		maskClosable: false,
		width: 480,
		platform: isPhoneVariant ? "h5" : void 0,
		mobileModalHeight: isPhoneVariant ? "auto" : void 0,
		className: `add-device-modal add-device-modal--${variant}`,
		bodyStyle: { padding: "24px" }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "add-device-modal__body" }, /* @__PURE__ */ react.default.createElement("div", { className: "add-device-modal__selected-model" }, (model === null || model === void 0 || (_model$type = model.type) === null || _model$type === void 0 ? void 0 : _model$type.icon) ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
		type: model === null || model === void 0 || (_model$type2 = model.type) === null || _model$type2 === void 0 ? void 0 : _model$type2.icon,
		style: { marginTop: "4px" }
	}) : null, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "add-device-modal__model-name" }, translation(model === null || model === void 0 ? void 0 : model.name) || modelCode || "-"), /* @__PURE__ */ react.default.createElement("div", { className: "add-device-modal__model-desc" }, translation(model === null || model === void 0 ? void 0 : model.description) || modelCode || "-"))), /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		preserve: false
	}, !workAreaId ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "workarea_id",
		label: getText("pisell2.saas.device-planning.edit.work-area"),
		required: true,
		rules: [{
			required: true,
			message: getText("pisell2.saas.device-planning.edit.work-area-required")
		}]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Select, { options: workAreas.map((item) => ({
		label: translation(item.name),
		value: item.id
	})) })) : null, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "name",
		label: getText("pisell2.saas.device-planning.add.device-name"),
		required: true,
		rules: [{ validator: require_types.validateI18nRequired(getText("pisell2.saas.device-planning.add.device-name-required")) }]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.BaseTranslation, { renderMode: "edit" })), profileRequired ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "profile_id",
		label: getText("pisell2.saas.device-planning.add.device-profile"),
		required: true,
		rules: [{
			required: true,
			message: getText("pisell2.saas.device-planning.add.device-profile-required")
		}],
		help: profiles.length === 0 ? getText("pisell2.saas.device-planning.add.create-profile-first") : void 0
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Select, {
		disabled: profiles.length === 0,
		options: profiles.map((profile) => ({
			label: translation(profile.name) || String(profile.id),
			value: profile.id
		}))
	})) : null)));
};
//#endregion
exports.default = AddDeviceModal;
