var w = Object.defineProperty, _ = Object.defineProperties;
var F = Object.getOwnPropertyDescriptors;
var h = Object.getOwnPropertySymbols;
var D = Object.prototype.hasOwnProperty, E = Object.prototype.propertyIsEnumerable;
var y = (e, t, r) => t in e ? w(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, v = (e, t) => {
  for (var r in t || (t = {}))
    D.call(t, r) && y(e, r, t[r]);
  if (h)
    for (var r of h(t))
      E.call(t, r) && y(e, r, t[r]);
  return e;
}, C = (e, t) => _(e, F(t));
import { ViteCodeInspectorPlugin as S } from "vite-code-inspector-plugin";
import W from "webpack-code-inspector-plugin";
import { isDev as j, getMappingFilePath as k, matchCondition as g, getCodeWithWebComponent as O, isJsTypeFile as M, parseSFC as R, transformCode as P, fileURLToPath as A } from "code-inspector-core";
import d from "fs";
import f, { dirname as L } from "path";
import N from "chalk";
import J from "dotenv";
var U = (e, t, r) => new Promise((l, c) => {
  var p = (o) => {
    try {
      i(r.next(o));
    } catch (s) {
      c(s);
    }
  }, n = (o) => {
    try {
      i(r.throw(o));
    } catch (s) {
      c(s);
    }
  }, i = (o) => o.done ? l(o.value) : Promise.resolve(o.value).then(p, n);
  i((r = r.apply(e, t)).next());
});
const V = "esbuild-code-inspector-plugin";
function $(e) {
  return {
    name: V,
    setup(t) {
      if (e.close || !j(e.dev, !1))
        return;
      const r = {
        port: 0,
        entry: "",
        output: e.output
      }, { escapeTags: l = [] } = e, c = /* @__PURE__ */ new Map();
      t.onLoad(
        { filter: e.match || /\.(jsx|tsx|js|ts|mjs|mts)?$/ },
        (p) => U(this, null, function* () {
          let n = p.path;
          n = k(n, e.mappings);
          let i = yield d.promises.readFile(n, "utf8"), o = c.get(n);
          if (!o || o.originCode !== i) {
            let s = i, u = e.exclude || [];
            Array.isArray(u) || (u = [u]);
            const T = g(
              [...u, /\/node_modules\//],
              n
            ), b = g(e.include || [], n);
            if (T && !b)
              return s;
            s = yield O({
              options: e,
              file: n,
              code: s,
              record: r
            });
            let a = "";
            if (M(n) ? a = "jsx" : n.endsWith(".svelte") && (a = "svelte"), a)
              s = P({
                content: s,
                filePath: n,
                fileType: a,
                escapeTags: l,
                pathType: e.pathType
              });
            else if (n.endsWith(".vue")) {
              a = "vue";
              const { descriptor: m } = R(s, {
                sourceMap: !1
              }), x = P({
                content: m.template.content,
                filePath: n,
                fileType: a,
                escapeTags: l,
                pathType: e.pathType
              });
              s = s.replace(m.template.content, x);
            }
            const I = f.extname(n).replace(".", "");
            o = { originCode: i, output: { contents: s, loader: I } }, c.set(n, o);
          }
          return o.output;
        })
      );
    }
  };
}
function q(e) {
  if (!(e != null && e.bundler)) {
    console.log(
      N.red(
        "Please specify the bundler in the options of code-inspector-plugin."
      )
    );
    return;
  }
  let t = !1;
  if (e.needEnvInspector)
    if (t = !0, process.env.CODE_INSPECTOR === "true")
      t = !1;
    else {
      const p = f.resolve(process.cwd(), ".env.local");
      if (d.existsSync(p)) {
        const n = d.readFileSync(p, "utf-8"), i = J.parse(n || "");
        (i == null ? void 0 : i.CODE_INSPECTOR) === "true" && (t = !1);
      }
    }
  let r = "";
  typeof __dirname != "undefined" ? r = __dirname : r = L(A(import.meta.url));
  const l = C(v({}, e), {
    close: t,
    output: f.resolve(r, "./")
  });
  return e.bundler === "webpack" || e.bundler === "rspack" ? new W(l) : e.bundler === "esbuild" ? $(l) : S(l);
}
const Z = q;
export {
  q as CodeInspectorPlugin,
  Z as codeInspectorPlugin
};
