"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var serverHandlers_exports = {};
__export(serverHandlers_exports, {
  getServerHandlers: () => getServerHandlers,
  serverHandlers: () => serverHandlers
});
module.exports = __toCommonJS(serverHandlers_exports);
var import_msw = require("msw");
var import_promises = require("fs/promises");
var import_node_path = __toESM(require("node:path"));
var import_app_config = __toESM(require("./app.config.json"));
var import_manifest = __toESM(require("./loan/latest/manifest.json"));
var import_manifest2 = __toESM(require("./task/latest/manifest.json"));
var import_manifest3 = __toESM(require("./travelhub/latest/manifest.json"));
var import_manifest4 = __toESM(require("./travelhub/23.1/manifest.json"));
var import_manifest5 = __toESM(require("./flights/latest/manifest.json"));
var import_manifest6 = __toESM(require("./flights/23.1/manifest.json"));
var import_manifest7 = __toESM(require("./hotels/latest/manifest.json"));
var import_manifest8 = __toESM(require("./hotels/23.1/manifest.json"));
var import_constants = require("./constants.js");
var import_utils = require("./utils.js");
const relMajorMinorVersion = (0, import_utils.getMajorMinorVersion)(import_constants.RELEASE_VERSION);
var ResourceType = /* @__PURE__ */ ((ResourceType2) => {
  ResourceType2["JS"] = "js";
  ResourceType2["CSS"] = "css";
  return ResourceType2;
})(ResourceType || {});
const manifests = {
  task: {
    latest: import_manifest2.default
  },
  travelHub: {
    latest: import_manifest3.default
  },
  flights: {
    latest: import_manifest5.default
  },
  hotels: {
    latest: import_manifest7.default
  }
};
manifests.task[relMajorMinorVersion] = {};
manifests.travelHub[relMajorMinorVersion] = import_manifest4.default;
manifests.flights[relMajorMinorVersion] = import_manifest6.default;
manifests.hotels[relMajorMinorVersion] = import_manifest8.default;
const sendResource = async (res, ctx, filePath, type = "js" /* JS */) => {
  const fileContent = await (0, import_promises.readFile)(import_node_path.default.join(__dirname, filePath), "utf-8");
  return res(
    ctx.status(200),
    ctx.set(
      "Content-Type",
      type === "js" /* JS */ ? "application/javascript" : "text/css"
    ),
    ctx.body(fileContent)
  );
};
const getAppResourceHandlers = (urls, baseFolder) => urls.map((url) => {
  const handler = import_msw.rest.get(url, async (req, res, ctx) => {
    const newRes = await sendResource(
      res,
      ctx,
      `${baseFolder}${url}`,
      url.includes(".css") ? "css" /* CSS */ : "js" /* JS */
    );
    return newRes;
  });
  return handler;
});
const sendJS = async (res, ctx, filePath) => {
  const fileContent = await (0, import_promises.readFile)(import_node_path.default.join(__dirname, filePath), "utf-8");
  return res(
    ctx.status(200),
    ctx.set("Content-Type", "application/javascript"),
    ctx.body(fileContent)
  );
};
const getServerHandlers = () => {
  const relVersion = (0, import_utils.getMajorMinorVersion)(import_constants.RELEASE_VERSION);
  const versionedHandlers = [import_constants.LATEST_VERSION, relVersion].reduce(
    (prevValue, version) => {
      prevValue = prevValue.concat([
        import_msw.rest.get(
          `/travelhub/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.travelHub[version]))
        ),
        import_msw.rest.get(
          `/subfolder/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.travelHub[version]))
        ),
        import_msw.rest.get(
          `/task/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.task[version]))
        ),
        import_msw.rest.get(
          `/flights/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.flights[version]))
        ),
        import_msw.rest.get(
          `/hotels/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.hotels[version]))
        ),
        ...getAppResourceHandlers(
          [
            `/task/${version}/index.js`,
            `/task/${version}/index.dev.js`,
            `/travelhub/${version}/app.checksum.js`,
            `/travelhub/${version}/app.checksum.css`,
            `/travelhub/${version}/landing.checksum.js`,
            `/travelhub/${version}/landing.checksum.css`,
            `/travelhub/${version}/landing.checksum1.js`,
            `/travelhub/${version}/landing.checksum1.css`,
            `/flights/${version}/app.checksum.js`,
            `/flights/${version}/app.checksum.css`,
            `/flights/${version}/app.checksum1.js`,
            `/flights/${version}/app.checksum1.css`,
            `/hotels/${version}/app.checksum.js`,
            `/hotels/${version}/app.checksum.css`,
            `/hotels/${version}/app.checksum1.js`,
            `/hotels/${version}/app.checksum1.css`
          ],
          "./"
        )
      ]);
      return prevValue;
    },
    []
  );
  return versionedHandlers;
};
const serverHandlers = [
  import_msw.rest.get(
    "https://www.google-analytics.com/analytics.js",
    (req, res, ctx) => res(
      ctx.status(200),
      ctx.set("Content-Type", "application/javascript"),
      ctx.body("")
    )
  ),
  import_msw.rest.get(
    "/latest/app.config.json",
    (req, res, ctx) => res(ctx.json(import_app_config.default))
  ),
  import_msw.rest.get(
    `/${relMajorMinorVersion}/app.config.json`,
    (req, res, ctx) => res(ctx.json(import_app_config.default))
  ),
  // frame html
  import_msw.rest.get("/iframe.html", async (req, res, ctx) => {
    const fileContent = await (0, import_promises.readFile)(
      import_node_path.default.join(__dirname, "../micro-iframe-app/iframe.html"),
      "utf-8"
    );
    return res(
      ctx.status(200),
      ctx.set("Content-Type", "text/html"),
      ctx.body(fileContent)
    );
  }),
  import_msw.rest.get("/frame.html", async (req, res, ctx) => {
    const fileContent = await (0, import_promises.readFile)(
      import_node_path.default.join(
        process.cwd(),
        "node_modules/@elliemae/pui-app-bridge/dist/esm/frame.html"
      ),
      "utf-8"
    );
    return res(
      ctx.status(200),
      ctx.set("Content-Type", "text/html"),
      ctx.body(fileContent)
    );
  }),
  import_msw.rest.get("/loan/latest/index.js", async (req, res, ctx) => {
    const newRes = await sendJS(res, ctx, "./loan/latest/index.js");
    return newRes;
  }),
  // loan microapp handler
  import_msw.rest.get(
    "/loan/latest/manifest.json",
    (req, res, ctx) => res(ctx.json(import_manifest.default))
  ),
  import_msw.rest.get("/ssfapp/", async (req, res, ctx) => {
    const fileContent = await (0, import_promises.readFile)(
      import_node_path.default.join(__dirname, "ssfapp/index.html"),
      "utf-8"
    );
    return res(
      ctx.status(200),
      ctx.set("Content-Type", "text/html"),
      ctx.body(fileContent)
    );
  }),
  import_msw.rest.get("/ssfapp/latest/index.js", async (req, res, ctx) => {
    const newRes = await sendJS(res, ctx, "./ssfapp/latest/index.js");
    return newRes;
  }),
  ...getServerHandlers()
];
