var f = (t, p, a) => new Promise((i, c) => {
  var u = (n) => {
    try {
      r(a.next(n));
    } catch (s) {
      c(s);
    }
  }, e = (n) => {
    try {
      r(a.throw(n));
    } catch (s) {
      c(s);
    }
  }, r = (n) => n.done ? i(n.value) : Promise.resolve(n.value).then(u, e);
  r((a = a.apply(t, p)).next());
});
import { isDev as T, getMappingFilePath as v, matchCondition as m, getCodeWithWebComponent as j, isJsTypeFile as F, transformCode as h, parseSFC as P } from "code-inspector-core";
import W from "fs";
import b from "path";
const w = "esbuild-code-inspector-plugin";
function D(t) {
  return {
    name: w,
    setup(p) {
      if (t.close || !T(t.dev, !1))
        return;
      const a = {
        port: 0,
        entry: "",
        output: t.output
      }, { escapeTags: i = [] } = t, c = /* @__PURE__ */ new Map();
      p.onLoad(
        { filter: t.match || /\.(jsx|tsx|js|ts|mjs|mts)?$/ },
        (u) => f(this, null, function* () {
          let e = u.path;
          e = v(e, t.mappings);
          let r = yield W.promises.readFile(e, "utf8"), n = c.get(e);
          if (!n || n.originCode !== r) {
            let s = r, o = t.exclude || [];
            Array.isArray(o) || (o = [o]);
            const g = m(
              [...o, /\/node_modules\//],
              e
            ), y = m(t.include || [], e);
            if (g && !y)
              return s;
            s = yield j({
              options: t,
              file: e,
              code: s,
              record: a
            });
            let l = "";
            if (F(e) ? l = "jsx" : e.endsWith(".svelte") && (l = "svelte"), l)
              s = h({
                content: s,
                filePath: e,
                fileType: l,
                escapeTags: i,
                pathType: t.pathType
              });
            else if (e.endsWith(".vue")) {
              l = "vue";
              const { descriptor: d } = P(s, {
                sourceMap: !1
              }), C = h({
                content: d.template.content,
                filePath: e,
                fileType: l,
                escapeTags: i,
                pathType: t.pathType
              });
              s = s.replace(d.template.content, C);
            }
            const x = b.extname(e).replace(".", "");
            n = { originCode: r, output: { contents: s, loader: x } }, c.set(e, n);
          }
          return n.output;
        })
      );
    }
  };
}
export {
  D as EsbuildCodeInspectorPlugin
};
