var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellFloorMapLayout/shopFloorPlan/shopFloorPlanClient.ts
var shopFloorPlanClient_exports = {};
__export(shopFloorPlanClient_exports, {
  SHOP_FLOOR_PLAN_DUPLICATE_CODE: () => import_shopFloorPlanTypes2.SHOP_FLOOR_PLAN_DUPLICATE_CODE,
  createShopFloorPlanClient: () => createShopFloorPlanClient
});
module.exports = __toCommonJS(shopFloorPlanClient_exports);
var import_shopFloorPlanPure = require("./shopFloorPlanPure");
var import_shopFloorPlanTypes = require("./shopFloorPlanTypes");
var import_shopFloorPlanTypes2 = require("./shopFloorPlanTypes");
async function fetchShopFloorPlanByCodeResponse(http, trimmedCode, subscribe) {
  const res = await http.get(
    `/shop/schedule/floor-plan/code/${encodeURIComponent(trimmedCode)}`,
    {},
    {
      fullResult: true,
      ...(subscribe == null ? void 0 : subscribe.subscriberId) != null && subscribe.subscriberId !== "" ? {
        isShopApi: true,
        abort: true,
        osServer: subscribe.osServer !== false,
        subscriberId: subscribe.subscriberId,
        ...subscribe.callback != null ? { callback: subscribe.callback } : {}
      } : {}
    }
  );
  return { res, detail: (0, import_shopFloorPlanPure.pickFloorPlanDetail)(res) };
}
function createShopFloorPlanClient(options) {
  const { http } = options;
  const saveFailed = options.saveFailedMessage ?? "Failed to save floor plan";
  async function postShopFloorPlan(body) {
    var _a;
    const res = await http.post("/shop/schedule/floor-plan", body, {
      fullResult: true
    });
    const ok = (res == null ? void 0 : res.code) === 200 || (res == null ? void 0 : res.status) === true || ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.status) === true;
    if (ok) {
      const detail = (0, import_shopFloorPlanPure.pickFloorPlanDetail)(res);
      if (detail) return detail;
      throw new Error(saveFailed);
    }
    if ((res == null ? 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, { [import_shopFloorPlanTypes.SHOP_FLOOR_PLAN_DUPLICATE_CODE]: true });
      throw err;
    }
    const msg = (res == null ? void 0 : res.message) || (typeof (res == null ? void 0 : res.data) === "object" && (res == null ? void 0 : res.data) && "message" in res.data && typeof res.data.message === "string" ? res.data.message : null) || saveFailed;
    throw new Error(msg);
  }
  async function ensureShopFloorPlanByCode(code, subscribe) {
    const trimmed = code == null ? void 0 : code.trim();
    if (!trimmed) return null;
    const { buildDefaultPostBody, ...subscribeRest } = subscribe ?? {};
    const postCreateOrRecover = async () => {
      if (!buildDefaultPostBody) return null;
      const body = buildDefaultPostBody(trimmed);
      try {
        return await postShopFloorPlan(body);
      } catch (e) {
        if ((0, import_shopFloorPlanPure.isFloorPlanDuplicateCodeError)(e)) {
          const { detail: again } = await fetchShopFloorPlanByCodeResponse(
            http,
            trimmed,
            subscribeRest
          );
          return again;
        }
        throw e;
      }
    };
    try {
      const { res, detail } = await fetchShopFloorPlanByCodeResponse(
        http,
        trimmed,
        subscribeRest
      );
      if (detail) return detail;
      if ((0, import_shopFloorPlanPure.isShopFloorPlanNotFoundResponse)(res)) {
        return await postCreateOrRecover();
      }
      return null;
    } catch (err) {
      if ((0, import_shopFloorPlanPure.isHttpNotFoundError)(err)) {
        return await postCreateOrRecover();
      }
      throw err;
    }
  }
  return {
    async unsubscribeShopFloorPlanCodeQuery(subscriberId) {
      if (!subscriberId) return;
      await http.get(
        "/shop/schedule/floor-plan/code/unsubscribe",
        { subscriberId },
        {
          isShopApi: true,
          osServer: true
        }
      );
    },
    async getShopFloorPlanByCode(code, subscribe) {
      const trimmed = code == null ? void 0 : code.trim();
      if (!trimmed) return null;
      const { detail } = await fetchShopFloorPlanByCodeResponse(
        http,
        trimmed,
        subscribe
      );
      return detail;
    },
    postShopFloorPlan,
    async putShopFloorPlan(id, body) {
      var _a;
      const res = await http.put(
        `/shop/schedule/floor-plan/${id}`,
        body,
        {
          fullResult: true
        }
      );
      const ok = (res == null ? void 0 : res.code) === 200 || (res == null ? void 0 : res.status) === true || ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.status) === true;
      if (!ok) {
        const msg = (res == null ? void 0 : res.message) || (typeof (res == null ? void 0 : res.data) === "object" && (res == null ? void 0 : res.data) && "message" in res.data && typeof res.data.message === "string" ? res.data.message : null) || saveFailed;
        throw new Error(msg);
      }
      return (res == null ? void 0 : res.data) ?? res;
    },
    ensureShopFloorPlanByCode
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SHOP_FLOOR_PLAN_DUPLICATE_CODE,
  createShopFloorPlanClient
});
