var w = Object.defineProperty, j = Object.defineProperties;
var P = Object.getOwnPropertyDescriptors;
var d = Object.getOwnPropertySymbols;
var W = Object.prototype.hasOwnProperty, q = Object.prototype.propertyIsEnumerable;
var h = (e, n, i) => n in e ? w(e, n, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[n] = i, u = (e, n) => {
  for (var i in n || (n = {}))
    W.call(n, i) && h(e, i, n[i]);
  if (d)
    for (var i of d(n))
      q.call(n, i) && h(e, i, n[i]);
  return e;
}, f = (e, n) => j(e, P(n));
var m = (e, n, i) => new Promise((t, a) => {
  var c = (l) => {
    try {
      p(i.next(l));
    } catch (r) {
      a(r);
    }
  }, s = (l) => {
    try {
      p(i.throw(l));
    } catch (r) {
      a(r);
    }
  }, p = (l) => l.done ? t(l.value) : Promise.resolve(l.value).then(c, s);
  p((i = i.apply(e, n)).next());
});
import { isDev as I, isExcludedFile as C, getCodeWithWebComponent as k, normalizePath as T, getMappingFilePath as b, isJsTypeFile as D, transformCode as R } from "@code-inspector/core";
import v from "chalk";
const x = "@code-inspector/vite", S = [
  {
    name: "vite:react-babel",
    package: "@vitejs/plugin-react"
  },
  {
    name: "vite:react-swc",
    package: "@vitejs/plugin-react-swc"
  },
  {
    name: "vite:react-oxc:config",
    package: "@vitejs/plugin-react-oxc"
  },
  {
    name: "solid",
    package: "vite-plugin-solid"
  },
  {
    name: "vite-plugin-qwik",
    package: "qwikVite"
  },
  {
    name: "vite-plugin-qwik-city",
    package: "qwikCity"
  },
  {
    name: "vite-plugin-qwik-react",
    package: "qwikReact"
  },
  {
    name: "vite:preact-jsx",
    package: "@preact/preset-vite"
  },
  {
    name: "vite-plugin-svelte",
    package: "@sveltejs/vite-plugin-svelte"
  }
], F = ["isJsx", "isTsx", "lang.jsx", "lang.tsx"];
function L(e = []) {
  const n = e.findIndex((i) => i.name === x);
  S.forEach((i) => {
    const t = e.findIndex((a) => a.name === i.name);
    if (t !== -1 && t < n) {
      const a = [
        v.yellow("[WARNING]"),
        "You need to put",
        v.green("code-inspector-plugin"),
        "before",
        v.green(i.package),
        "in the vite config file."
      ];
      console.log(a.join(" "));
    }
  });
}
function J(e) {
  const n = {
    port: 0,
    entry: "",
    output: e.output,
    envDir: ""
  };
  return f(u({
    name: x
  }, e.enforcePre === !1 ? {} : { enforce: "pre" }), {
    apply(t, { command: a }) {
      return !e.close && I(e.dev, a === "serve");
    },
    configResolved(t) {
      n.envDir = t.envDir || t.root, n.root = t.root;
    },
    transform(t, a) {
      return m(this, null, function* () {
        if (C(a, e))
          return t;
        t = yield k({
          options: e,
          file: a,
          code: t,
          record: n
        });
        const { escapeTags: c = [], mappings: s } = e, [p, l] = a.split("?", 2);
        let r = T(p);
        r = b(r, s);
        const o = new URLSearchParams(l);
        if (e.match && !e.match.test(r))
          return t;
        let g = "";
        return D(r) || r.endsWith(".vue") && (F.some((y) => o.get(y) !== null) || o.get("lang") === "tsx" || o.get("lang") === "jsx") ? g = "jsx" : r.endsWith(".html") && o.get("type") === "template" && o.has("vue") || r.endsWith(".vue") && o.get("type") !== "style" && o.get("raw") === null ? g = "vue" : r.endsWith(".svelte") && (g = "svelte"), g ? R({
          content: t,
          filePath: r,
          fileType: g,
          escapeTags: c,
          pathType: e.pathType
        }) : t;
      });
    },
    // 追加到 html 中，适配 MPA 项目
    transformIndexHtml(t) {
      return m(this, null, function* () {
        var c, s;
        if ((s = (c = e.skipSnippets) == null ? void 0 : c.includes) != null && s.call(c, "htmlScript"))
          return t;
        const a = yield k({
          options: f(u({}, e), { importClient: "code" }),
          file: "main.js",
          code: "",
          record: n,
          inject: !0
        });
        return t.replace(
          "<head>",
          '<head><script type="module">\n'.concat(a, "\n<\/script>")
        );
      });
    },
    configureServer(t) {
      const a = t.config.logger.info;
      t.config.logger.info = function(c, s) {
        a.call(this, c, s), L(t.config.plugins), t.config.logger.info = a;
      };
    }
  });
}
export {
  J as ViteCodeInspectorPlugin
};
