import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { toLangDict, validateI18nRequired } from "../../types.js";
import React, { useEffect, useMemo } from "react";
import { Alert, Form, Input } from "antd";
import { BaseTranslation, PisellModal, Select as Select$1 } from "@pisell/materials";
import { ExclamationCircleFilled } from "@ant-design/icons";
import "./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] = 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";
	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: values.profile_id || 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: {
			loading: submitting,
			onClick: handleOk
		},
		cancelButtonProps: { 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(Alert, {
		type: "warning",
		showIcon: true,
		icon: /* @__PURE__ */ React.createElement(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.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(Form.Item, {
		name: "name",
		label: getText("pisell2.saas.device-planning.detail.name"),
		required: true,
		rules: [{ validator: validateI18nRequired(getText("pisell2.saas.device-planning.add.device-name-required")) }]
	}, /* @__PURE__ */ React.createElement(BaseTranslation, { renderMode: "edit" })), 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 };
