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 { request } from "../../../utils/index.js";
import { RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND, RESERVATION_BOUND_TABLE_ELEMENT_KIND } from "./reservationMock.js";
import create_floor_map_default from "../api/create-floor-map.js";
import { locales } from "@pisell/utils";
import { PisellToast, buildFloorPlanPutBody, createShopFloorPlanClient, shopLayoutToSceneElements } from "@pisell/materials";
//#region src/plus/pisellReservation/floorMap/floorPlanApi.ts
/**
* 店铺平面图：在 {@link createShopFloorPlanClient}（materials）之上绑定引擎 `request`，
* 并保留预约专用的默认 POST 体、Toast 与 {@link shopLayoutToSceneElements} 图元种类默认值。
*/
const I18N_FLOOR_PLAN_SAVE_FAILED = "pisell-reservation.floor-plan.save-failed";
const PISELL_RESERVATION_FLOOR_PLAN_CODE = "pisell_reservation";
const shopClient = 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: locales.getText(I18N_FLOOR_PLAN_SAVE_FAILED)
});
shopClient.getShopFloorPlanByCode.bind(shopClient);
shopClient.postShopFloorPlan.bind(shopClient);
const putShopFloorPlan = shopClient.putShopFloorPlan.bind(shopClient);
const unsubscribeShopFloorPlanCodeQuery = shopClient.unsubscribeShopFloorPlanCodeQuery.bind(shopClient);
/**
* 404 时 POST 创建使用预约默认 JSON（`api/create-floor-map.json`）。
*/
function ensureShopFloorPlanByCode(_x, _x2) {
	return _ensureShopFloorPlanByCode.apply(this, arguments);
}
function _ensureShopFloorPlanByCode() {
	_ensureShopFloorPlanByCode = _asyncToGenerator(function* (code, subscribe) {
		return shopClient.ensureShopFloorPlanByCode(code, _objectSpread2(_objectSpread2({}, subscribe), {}, { buildDefaultPostBody: buildDefaultReservationFloorPlanPostBody }));
	});
	return _ensureShopFloorPlanByCode.apply(this, arguments);
}
function buildDefaultReservationFloorPlanPostBody(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_floor_map_default;
	return _objectSpread2(_objectSpread2({
		code: trimmed,
		name: _objectSpread2({}, j.name),
		layout: JSON.parse(JSON.stringify(j.layout)),
		canvas_width: j.canvas_width,
		canvas_height: j.canvas_height
	}, j.sort !== void 0 ? { sort: j.sort } : {}), j.status !== void 0 ? { status: j.status } : {});
}
/**
* 将扁平 layout 转为 sceneElements；圆/方桌 elementKind 使用预约默认。
*/
function shopLayoutToSceneElements$1(layout, options) {
	return shopLayoutToSceneElements(layout, {
		canvasId: options.canvasId,
		dataSourceKey: options.dataSourceKey,
		rectElementKind: RESERVATION_BOUND_TABLE_ELEMENT_KIND,
		circleElementKind: RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND
	});
}
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 { PISELL_RESERVATION_FLOOR_PLAN_CODE, ensureShopFloorPlanByCode, saveViewConfigToShopFloorPlan, shopLayoutToSceneElements$1 as shopLayoutToSceneElements, toastFloorPlanLoadError, toastFloorPlanSaveError, unsubscribeShopFloorPlanCodeQuery };
