import { rest } from "msw";
import { readFile } from "fs/promises";
import path from "node:path";
import appConfig from "./app.config.json";
import loanAppManifest from "./loan/latest/manifest.json";
import latestTaskManifest from "./task/latest/manifest.json";
import latestAppManifest from "./travelhub/latest/manifest.json";
import versionedAppManifest from "./travelhub/23.1/manifest.json";
import latestFlightsManifest from "./flights/latest/manifest.json";
import versionedFlightsManifest from "./flights/23.1/manifest.json";
import latestHotelsManifest from "./hotels/latest/manifest.json";
import versionedHotelsManifest from "./hotels/23.1/manifest.json";
import { LATEST_VERSION, RELEASE_VERSION } from "./constants.js";
import { getMajorMinorVersion } from "./utils.js";
const relMajorMinorVersion = getMajorMinorVersion(RELEASE_VERSION);
var ResourceType = /* @__PURE__ */ ((ResourceType2) => {
  ResourceType2["JS"] = "js";
  ResourceType2["CSS"] = "css";
  return ResourceType2;
})(ResourceType || {});
const manifests = {
  task: {
    latest: latestTaskManifest
  },
  travelHub: {
    latest: latestAppManifest
  },
  flights: {
    latest: latestFlightsManifest
  },
  hotels: {
    latest: latestHotelsManifest
  }
};
manifests.task[relMajorMinorVersion] = {};
manifests.travelHub[relMajorMinorVersion] = versionedAppManifest;
manifests.flights[relMajorMinorVersion] = versionedFlightsManifest;
manifests.hotels[relMajorMinorVersion] = versionedHotelsManifest;
const sendResource = async (res, ctx, filePath, type = "js" /* JS */) => {
  const fileContent = await readFile(path.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 = 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 readFile(path.join(__dirname, filePath), "utf-8");
  return res(
    ctx.status(200),
    ctx.set("Content-Type", "application/javascript"),
    ctx.body(fileContent)
  );
};
const getServerHandlers = () => {
  const relVersion = getMajorMinorVersion(RELEASE_VERSION);
  const versionedHandlers = [LATEST_VERSION, relVersion].reduce(
    (prevValue, version) => {
      prevValue = prevValue.concat([
        rest.get(
          `/travelhub/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.travelHub[version]))
        ),
        rest.get(
          `/subfolder/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.travelHub[version]))
        ),
        rest.get(
          `/task/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.task[version]))
        ),
        rest.get(
          `/flights/${version}/manifest.json`,
          (req, res, ctx) => res(ctx.json(manifests.flights[version]))
        ),
        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 = [
  rest.get(
    "https://www.google-analytics.com/analytics.js",
    (req, res, ctx) => res(
      ctx.status(200),
      ctx.set("Content-Type", "application/javascript"),
      ctx.body("")
    )
  ),
  rest.get(
    "/latest/app.config.json",
    (req, res, ctx) => res(ctx.json(appConfig))
  ),
  rest.get(
    `/${relMajorMinorVersion}/app.config.json`,
    (req, res, ctx) => res(ctx.json(appConfig))
  ),
  // frame html
  rest.get("/iframe.html", async (req, res, ctx) => {
    const fileContent = await readFile(
      path.join(__dirname, "../micro-iframe-app/iframe.html"),
      "utf-8"
    );
    return res(
      ctx.status(200),
      ctx.set("Content-Type", "text/html"),
      ctx.body(fileContent)
    );
  }),
  rest.get("/frame.html", async (req, res, ctx) => {
    const fileContent = await readFile(
      path.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)
    );
  }),
  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
  rest.get(
    "/loan/latest/manifest.json",
    (req, res, ctx) => res(ctx.json(loanAppManifest))
  ),
  rest.get("/ssfapp/", async (req, res, ctx) => {
    const fileContent = await readFile(
      path.join(__dirname, "ssfapp/index.html"),
      "utf-8"
    );
    return res(
      ctx.status(200),
      ctx.set("Content-Type", "text/html"),
      ctx.body(fileContent)
    );
  }),
  rest.get("/ssfapp/latest/index.js", async (req, res, ctx) => {
    const newRes = await sendJS(res, ctx, "./ssfapp/latest/index.js");
    return newRes;
  }),
  ...getServerHandlers()
];
export {
  getServerHandlers,
  serverHandlers
};
