import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { SHOP_FLOOR_PLAN_DUPLICATE_CODE } from "./shopFloorPlanTypes.js";
import { isFloorPlanDuplicateCodeError, isHttpNotFoundError, isShopFloorPlanNotFoundResponse, pickFloorPlanDetail } from "./shopFloorPlanPure.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
//#region src/components/pisellFloorMapLayout/shopFloorPlan/shopFloorPlanClient.ts
const _excluded = ["buildDefaultPostBody"];
function fetchShopFloorPlanByCodeResponse(_x, _x2, _x3) {
	return _fetchShopFloorPlanByCodeResponse.apply(this, arguments);
}
function _fetchShopFloorPlanByCodeResponse() {
	_fetchShopFloorPlanByCodeResponse = _asyncToGenerator(function* (http, trimmedCode, subscribe) {
		const res = yield http.get(`/shop/schedule/floor-plan/code/${encodeURIComponent(trimmedCode)}`, {}, _objectSpread2({
			fullResult: true,
			isShopApi: true
		}, (subscribe === null || subscribe === void 0 ? void 0 : subscribe.subscriberId) != null && subscribe.subscriberId !== "" ? _objectSpread2({
			abort: true,
			osServer: subscribe.osServer !== false,
			subscriberId: subscribe.subscriberId
		}, subscribe.callback != null ? { callback: subscribe.callback } : {}) : {}));
		return {
			res,
			detail: pickFloorPlanDetail(res)
		};
	});
	return _fetchShopFloorPlanByCodeResponse.apply(this, arguments);
}
function createShopFloorPlanClient(options) {
	var _options$saveFailedMe;
	const { http } = options;
	const saveFailed = (_options$saveFailedMe = options.saveFailedMessage) !== null && _options$saveFailedMe !== void 0 ? _options$saveFailedMe : "Failed to save floor plan";
	function postShopFloorPlan(_x4) {
		return _postShopFloorPlan.apply(this, arguments);
	}
	function _postShopFloorPlan() {
		_postShopFloorPlan = _asyncToGenerator(function* (body) {
			var _data;
			const res = yield http.post("/shop/schedule/floor-plan", body, {
				fullResult: true,
				isShopApi: true
			});
			if ((res === null || res === void 0 ? void 0 : res.code) === 200 || (res === null || res === void 0 ? void 0 : res.status) === true || (res === null || res === void 0 || (_data = res.data) === null || _data === void 0 ? void 0 : _data.status) === true) {
				const detail = pickFloorPlanDetail(res);
				if (detail) return detail;
				throw new Error(saveFailed);
			}
			if ((res === null || res === void 0 ? void 0 : res.code) === 5001) {
				const err = new Error(typeof res.message === "string" && res.message.trim() ? res.message : "Floor plan code already exists");
				Object.assign(err, { [SHOP_FLOOR_PLAN_DUPLICATE_CODE]: true });
				throw err;
			}
			const msg = (res === null || res === void 0 ? void 0 : res.message) || (typeof (res === null || res === void 0 ? void 0 : res.data) === "object" && (res === null || res === void 0 ? void 0 : res.data) && "message" in res.data && typeof res.data.message === "string" ? res.data.message : null) || saveFailed;
			throw new Error(msg);
		});
		return _postShopFloorPlan.apply(this, arguments);
	}
	function ensureShopFloorPlanByCode(_x5, _x6) {
		return _ensureShopFloorPlanByCode.apply(this, arguments);
	}
	function _ensureShopFloorPlanByCode() {
		_ensureShopFloorPlanByCode = _asyncToGenerator(function* (code, subscribe) {
			const trimmed = code === null || code === void 0 ? void 0 : code.trim();
			if (!trimmed) return null;
			const _ref = subscribe !== null && subscribe !== void 0 ? subscribe : {}, { buildDefaultPostBody } = _ref, subscribeRest = _objectWithoutProperties(_ref, _excluded);
			const postCreateOrRecover = function() {
				var _ref2 = _asyncToGenerator(function* () {
					if (!buildDefaultPostBody) return null;
					const body = buildDefaultPostBody(trimmed);
					try {
						return yield postShopFloorPlan(body);
					} catch (e) {
						if (isFloorPlanDuplicateCodeError(e)) {
							const { detail: again } = yield fetchShopFloorPlanByCodeResponse(http, trimmed, subscribeRest);
							return again;
						}
						throw e;
					}
				});
				return function postCreateOrRecover() {
					return _ref2.apply(this, arguments);
				};
			}();
			try {
				const { res, detail } = yield fetchShopFloorPlanByCodeResponse(http, trimmed, subscribeRest);
				if (detail) return detail;
				if (isShopFloorPlanNotFoundResponse(res)) return yield postCreateOrRecover();
				return null;
			} catch (err) {
				if (isHttpNotFoundError(err)) return yield postCreateOrRecover();
				throw err;
			}
		});
		return _ensureShopFloorPlanByCode.apply(this, arguments);
	}
	return {
		unsubscribeShopFloorPlanCodeQuery(subscriberId) {
			return _asyncToGenerator(function* () {
				if (!subscriberId) return;
				yield http.get("/shop/schedule/floor-plan/code/unsubscribe", { subscriberId }, {
					isShopApi: true,
					osServer: true
				});
			})();
		},
		getShopFloorPlanByCode(code, subscribe) {
			return _asyncToGenerator(function* () {
				const trimmed = code === null || code === void 0 ? void 0 : code.trim();
				if (!trimmed) return null;
				const { detail } = yield fetchShopFloorPlanByCodeResponse(http, trimmed, subscribe);
				return detail;
			})();
		},
		postShopFloorPlan,
		putShopFloorPlan(id, body) {
			return _asyncToGenerator(function* () {
				var _data2, _res$data;
				const res = yield http.put(`/shop/schedule/floor-plan/${id}`, body, {
					fullResult: true,
					isShopApi: true
				});
				if (!((res === null || res === void 0 ? void 0 : res.code) === 200 || (res === null || res === void 0 ? void 0 : res.status) === true || (res === null || res === void 0 || (_data2 = res.data) === null || _data2 === void 0 ? void 0 : _data2.status) === true)) {
					const msg = (res === null || res === void 0 ? void 0 : res.message) || (typeof (res === null || res === void 0 ? void 0 : res.data) === "object" && (res === null || res === void 0 ? void 0 : res.data) && "message" in res.data && typeof res.data.message === "string" ? res.data.message : null) || saveFailed;
					throw new Error(msg);
				}
				return (_res$data = res === null || res === void 0 ? void 0 : res.data) !== null && _res$data !== void 0 ? _res$data : res;
			})();
		},
		ensureShopFloorPlanByCode
	};
}
//#endregion
export { createShopFloorPlanClient };
