const require_runtime = require("../../../../../_virtual/_rolldown/runtime.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");
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/plus/saasDevice/devicePlanning/components/EditDeviceModal/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 EditDeviceModal = ({ open, record, workAreas, models, submitting, getText, translation, onCancel, onSubmit }) => {
	const [form] = antd.Form.useForm();
	const currentModel = (0, react.useMemo)(() => {
		if (!record) return void 0;
		return models.find((item) => item.code === record._raw.model_code) || record._raw.model;
	}, [models, record]);
	const profileRequired = isProfileRequired(currentModel);
	const profiles = (currentModel === null || currentModel === void 0 ? void 0 : currentModel.active_profiles) || [];
	const online = (record === null || record === void 0 ? void 0 : record.status) === "online";
	(0, react.useEffect)(() => {
		if (!open || !record) return;
		form.setFieldsValue({
			workarea_id: record._raw.workarea_id,
			name: require_types.toLangDict(record._raw.name),
			profile_id: record._raw.profile_id || void 0
		});
	}, [
		form,
		open,
		record
	]);
	const handleOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (!record) return;
			const values = yield form.validateFields();
			onSubmit(record.id, {
				workarea_id: values.workarea_id,
				name: values.name,
				profile_id: values.profile_id || null
			});
		});
		return function handleOk() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open,
		title: getText("pisell2.saas.device-planning.edit.title"),
		headerDivider: false,
		onCancel,
		okText: getText("pisell2.saas.device-planning.detail.save"),
		cancelText: getText("pisell2.saas.device-planning.detail.cancel"),
		okButtonProps: {
			loading: submitting,
			onClick: handleOk
		},
		cancelButtonProps: { disabled: submitting },
		destroyOnClose: true,
		maskClosable: false,
		width: 520,
		className: "edit-device-modal"
	}, record ? /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		preserve: false
	}, online ? /* @__PURE__ */ react.default.createElement(antd.Alert, {
		type: "warning",
		showIcon: true,
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.ExclamationCircleFilled, null),
		message: getText("pisell2.saas.device-planning.edit.online-title"),
		description: getText("pisell2.saas.device-planning.edit.online-desc"),
		style: { marginBottom: 16 }
	}) : null, /* @__PURE__ */ react.default.createElement("div", { className: "edit-device-modal__row" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: getText("pisell2.saas.device-planning.detail.type"),
		className: "edit-device-modal__col"
	}, /* @__PURE__ */ react.default.createElement(antd.Input, {
		value: record.typeName,
		disabled: true
	})), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: getText("pisell2.saas.device-planning.detail.model"),
		className: "edit-device-modal__col"
	}, /* @__PURE__ */ react.default.createElement(antd.Input, {
		value: record.modelName,
		disabled: true
	}))), /* @__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
	})) })), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "name",
		label: getText("pisell2.saas.device-planning.detail.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 || profiles.length > 0 ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "profile_id",
		label: getText("pisell2.saas.device-planning.detail.profile"),
		help: profileRequired && 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) : null);
};
//#endregion
exports.default = EditDeviceModal;
