import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { request } from "../../../utils/index.js";
import { getTopologyText, resolveTopologyDisplayLocale } from "../topologyLocale.js";
import create_topology_floor_map_default from "../api/create-topology-floor-map.js";
import { getTopologyFloorMapShellConfig } from "../topologyMock.js";
import { PisellToast, buildFloorPlanPutBody, createShopFloorPlanClient, viewConfigToLayoutPayload } from "@pisell/materials";
//#region src/plus/pisellTopology/floorPlan/topologyFloorPlanApi.ts
/**
* 拓扑页 shop floor-plan API（绑定引擎 request）
*/
const _excluded = ["buildDefaultPostBody"];
const client = createShopFloorPlanClient({
	http: {
		get: (url, params, config) => request.getRequest().get(url, params !== null && params !== void 0 ? params : {}, config !== null && config !== void 0 ? config : {}),
		post: (url, body, config) => request.getRequest().post(url, body !== null && body !== void 0 ? body : {}, config !== null && config !== void 0 ? config : {}),
		put: (url, body, config) => request.getRequest().put(url, body !== null && body !== void 0 ? body : {}, config !== null && config !== void 0 ? config : {})
	},
	saveFailedMessage: getTopologyText("pisell-topology.floor-plan.save-failed", resolveTopologyDisplayLocale(void 0)) || "Failed to save topology floor plan"
});
client.getShopFloorPlanByCode.bind(client);
const putShopFloorPlan = client.putShopFloorPlan.bind(client);
const unsubscribeShopFloorPlanCodeQuery = client.unsubscribeShopFloorPlanCodeQuery.bind(client);
function buildDefaultTopologyFloorPlanPostBody(code) {
	const trimmed = code === null || code === void 0 ? void 0 : code.trim();
	if (!trimmed) throw new Error("floor plan code is required");
	const j = create_topology_floor_map_default;
	return _objectSpread2(_objectSpread2({
		code: trimmed,
		name: _objectSpread2({}, j.name),
		layout: viewConfigToLayoutPayload(getTopologyFloorMapShellConfig()),
		canvas_width: j.canvas_width,
		canvas_height: j.canvas_height
	}, j.sort !== void 0 ? { sort: j.sort } : {}), j.status !== void 0 ? { status: j.status } : {});
}
function ensureShopFloorPlanByCode(_x, _x2) {
	return _ensureShopFloorPlanByCode.apply(this, arguments);
}
function _ensureShopFloorPlanByCode() {
	_ensureShopFloorPlanByCode = _asyncToGenerator(function* (code, subscribe) {
		const _ref = subscribe !== null && subscribe !== void 0 ? subscribe : {}, { buildDefaultPostBody } = _ref, rest = _objectWithoutProperties(_ref, _excluded);
		return client.ensureShopFloorPlanByCode(code, _objectSpread2(_objectSpread2({}, rest), {}, { buildDefaultPostBody: buildDefaultPostBody !== null && buildDefaultPostBody !== void 0 ? buildDefaultPostBody : buildDefaultTopologyFloorPlanPostBody }));
	});
	return _ensureShopFloorPlanByCode.apply(this, arguments);
}
function saveViewConfigToShopFloorPlan(_x3, _x4, _x5) {
	return _saveViewConfigToShopFloorPlan.apply(this, arguments);
}
function _saveViewConfigToShopFloorPlan() {
	_saveViewConfigToShopFloorPlan = _asyncToGenerator(function* (id, config, options) {
		var _options$putFloorPlan;
		const body = buildFloorPlanPutBody(config, options);
		return ((_options$putFloorPlan = options.putFloorPlan) !== null && _options$putFloorPlan !== void 0 ? _options$putFloorPlan : putShopFloorPlan)(id, body);
	});
	return _saveViewConfigToShopFloorPlan.apply(this, arguments);
}
function toastFloorPlanSaveError(err) {
	PisellToast({ content: err instanceof Error ? err.message : String(err) });
}
function toastFloorPlanLoadError(err) {
	PisellToast({ content: err instanceof Error ? err.message : String(err) });
}
//#endregion
export { ensureShopFloorPlanByCode, saveViewConfigToShopFloorPlan, toastFloorPlanLoadError, toastFloorPlanSaveError, unsubscribeShopFloorPlanCodeQuery };
