import { _asyncToGenerator } from "../../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { toLangDict } from "../../../../model/types.js";
import { isProfileRequired } from "../../../../shared/utils.js";
import I18nNameFormItem from "../../../../components/shared/I18nNameFormItem.js";
import OnlineWarningAlert from "../../../../components/shared/OnlineWarningAlert.js";
import React, { useEffect, useMemo } from "react";
import { Form, Input } from "antd";
import { PisellModal, Select as Select$1 } from "@pisell/materials";
import "./index.less";
//#region src/plus/saasDevice/devicePlanning/modules/devices/components/EditDeviceModal/index.tsx
const EditDeviceModal = ({ open, record, workAreas, models, submitting, getText, translation, onCancel, onSubmit }) => {
	const [form] = Form.useForm();
	const currentModel = 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";
	const showProfile = (record === null || record === void 0 ? void 0 : record._raw.type_kind) === "smart";
	useEffect(() => {
		if (!open || !record) return;
		form.setFieldsValue({
			workarea_id: record._raw.workarea_id,
			name: toLangDict(record._raw.name),
			profile_id: record._raw.profile_id || void 0
		});
	}, [
		form,
		open,
		record
	]);
	const handleOk = function() {
		var _ref = _asyncToGenerator(function* () {
			if (!record) return;
			const values = yield form.validateFields();
			onSubmit(record.id, {
				workarea_id: values.workarea_id,
				name: values.name,
				profile_id: showProfile ? values.profile_id || null : null
			});
		});
		return function handleOk() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(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: {
			block: true,
			size: "large",
			loading: submitting,
			onClick: handleOk
		},
		cancelButtonProps: {
			block: true,
			size: "large",
			type: "default",
			disabled: submitting
		},
		destroyOnClose: true,
		maskClosable: false,
		width: 520,
		className: "edit-device-modal"
	}, record ? /* @__PURE__ */ React.createElement(Form, {
		form,
		layout: "vertical",
		preserve: false
	}, online ? /* @__PURE__ */ React.createElement(OnlineWarningAlert, {
		message: getText("pisell2.saas.device-planning.edit.online-title"),
		description: getText("pisell2.saas.device-planning.edit.online-desc"),
		style: { marginBottom: 16 }
	}) : null, /* @__PURE__ */ React.createElement("div", { className: "edit-device-modal__row" }, /* @__PURE__ */ React.createElement(Form.Item, {
		label: getText("pisell2.saas.device-planning.detail.type"),
		className: "edit-device-modal__col"
	}, /* @__PURE__ */ React.createElement(Input, {
		value: record.typeName,
		disabled: true
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		label: getText("pisell2.saas.device-planning.detail.model"),
		className: "edit-device-modal__col"
	}, /* @__PURE__ */ React.createElement(Input, {
		value: record.modelName,
		disabled: true
	}))), /* @__PURE__ */ React.createElement(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.createElement(Select$1, { options: workAreas.map((item) => ({
		label: translation(item.name),
		value: item.id
	})) })), /* @__PURE__ */ React.createElement(I18nNameFormItem, {
		name: "name",
		label: getText("pisell2.saas.device-planning.detail.name"),
		requiredMessage: getText("pisell2.saas.device-planning.add.device-name-required")
	}), showProfile && (profileRequired || profiles.length > 0) ? /* @__PURE__ */ React.createElement(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.createElement(Select$1, {
		disabled: profiles.length === 0,
		options: profiles.map((profile) => ({
			label: translation(profile.name) || String(profile.id),
			value: profile.id
		}))
	})) : null) : null);
};
//#endregion
export { EditDeviceModal as default };
