var T = Object.defineProperty, W = Object.defineProperties;
var w = Object.getOwnPropertyDescriptors;
var g = Object.getOwnPropertySymbols;
var I = Object.prototype.hasOwnProperty, _ = Object.prototype.propertyIsEnumerable;
var y = (e, t, r) => t in e ? T(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, d = (e, t) => {
  for (var r in t || (t = {}))
    I.call(t, r) && y(e, r, t[r]);
  if (g)
    for (var r of g(t))
      _.call(t, r) && y(e, r, t[r]);
  return e;
}, h = (e, t) => W(e, w(t));
var o = (e, t, r) => new Promise((n, s) => {
  var c = (a) => {
    try {
      m(r.next(a));
    } catch (p) {
      s(p);
    }
  }, f = (a) => {
    try {
      m(r.throw(a));
    } catch (p) {
      s(p);
    }
  }, m = (a) => a.done ? n(a.value) : Promise.resolve(a.value).then(c, f);
  m((r = r.apply(e, t)).next());
});
import { isDev as A, matchCondition as x, getCodeWithWebComponent as v, normalizePath as F, getMappingFilePath as J, isJsTypeFile as L, transformCode as b } from "code-inspector-core";
const q = "vite-code-inspector-plugin", z = ["isJsx", "isTsx", "lang.jsx", "lang.tsx"];
function H(e) {
  const t = {
    port: 0,
    entry: "",
    output: e.output
  };
  return h(d({
    name: q
  }, e.enforcePre === !1 ? {} : { enforce: "pre" }), {
    apply(n, { command: s }) {
      return !e.close && A(e.dev, s === "serve");
    },
    transform(n, s) {
      return o(this, null, function* () {
        let c = e.exclude || [];
        Array.isArray(c) || (c = [c]);
        const f = x([...c, /\/node_modules\//], s), m = x(e.include || [], s);
        if (f && !m)
          return n;
        n = yield v({
          options: e,
          file: s,
          code: n,
          record: t
        });
        const { escapeTags: a = [], mappings: p } = e, [P, j] = s.split("?", 2);
        let l = F(P);
        l = J(l, p);
        const i = new URLSearchParams(j);
        if (e.match && !e.match.test(l))
          return n;
        let u = "";
        return L(l) || l.endsWith(".vue") && (z.some((C) => i.get(C) !== null) || i.get("lang") === "tsx" || i.get("lang") === "jsx") ? u = "jsx" : l.endsWith(".html") && i.get("type") === "template" && i.has("vue") || l.endsWith(".vue") && i.get("type") !== "style" && i.get("raw") === null ? u = "vue" : l.endsWith(".svelte") && (u = "svelte"), u ? b({
          content: n,
          filePath: l,
          fileType: u,
          escapeTags: a,
          pathType: e.pathType
        }) : n;
      });
    },
    // 追加到 html 中，适配 MPA 项目
    transformIndexHtml(n) {
      return o(this, null, function* () {
        const s = yield v({
          options: h(d({}, e), { importClient: "code" }),
          file: "main.js",
          code: "",
          record: t,
          inject: !0
        });
        return n.replace(
          "<head>",
          '<head><script type="module">\n'.concat(s, "\n<\/script>")
        );
      });
    }
  });
}
export {
  H as ViteCodeInspectorPlugin
};
