const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../utils.js");
const require_types = require("../types.js");
const require_I18nNameFormItem = require("./shared/I18nNameFormItem.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
//#region src/plus/saasDevice/appPeripheral/components/EditDeviceModal.tsx
const EditDeviceModal = ({ open, record, variant, workAreas, models, submitting, getText, translation, onCancel, onSubmit }) => {
	const [form] = antd.Form.useForm();
	const isPhoneVariant = variant === "phone";
	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 = require_utils.isProfileRequired(currentModel);
	const profiles = (currentModel === null || currentModel === void 0 ? void 0 : currentModel.active_profiles) || [];
	(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: 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: {
			block: true,
			size: "large",
			loading: submitting,
			onClick: handleOk
		},
		cancelButtonProps: {
			block: true,
			size: "large",
			type: "default",
			disabled: submitting
		},
		destroyOnClose: true,
		maskClosable: false,
		width: isPhoneVariant ? "100%" : 520,
		platform: isPhoneVariant ? "h5" : void 0,
		mobileModalHeight: isPhoneVariant ? "auto" : void 0,
		className: (0, classnames.default)("saas-app-peripheral-edit-device-modal", { "saas-app-peripheral-edit-device-modal--phone": isPhoneVariant })
	}, record ? /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		preserve: false
	}, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-peripheral-edit-device-modal__row" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: getText("pisell2.saas.device-planning.detail.type"),
		className: "saas-app-peripheral-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: "saas-app-peripheral-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(require_I18nNameFormItem.default, {
		name: "name",
		label: getText("pisell2.saas.device-planning.detail.name"),
		requiredMessage: getText("pisell2.saas.device-planning.add.device-name-required")
	}), profileRequired && profiles.length > 0 ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "profile_id",
		label: getText("pisell2.saas.device-planning.detail.profile")
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Select, { options: profiles.map((profile) => ({
		label: translation(profile.name) || String(profile.id),
		value: profile.id
	})) })) : null) : null);
};
//#endregion
exports.default = EditDeviceModal;
