var W = Object.defineProperty, D = Object.defineProperties;
var E = Object.getOwnPropertyDescriptors;
var b = Object.getOwnPropertySymbols;
var T = Object.prototype.hasOwnProperty, L = Object.prototype.propertyIsEnumerable;
var P = (t, e, s) => e in t ? W(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s, d = (t, e) => {
  for (var s in e || (e = {}))
    T.call(e, s) && P(t, s, e[s]);
  if (b)
    for (var s of b(e))
      L.call(e, s) && P(t, s, e[s]);
  return t;
}, j = (t, e) => D(t, E(e));
var y = (t, e, s) => new Promise((o, a) => {
  var r = (n) => {
    try {
      u(s.next(n));
    } catch (f) {
      a(f);
    }
  }, i = (n) => {
    try {
      u(s.throw(n));
    } catch (f) {
      a(f);
    }
  }, u = (n) => n.done ? o(n.value) : Promise.resolve(n.value).then(r, i);
  u((s = s.apply(t, e)).next());
});
import { normalizePath as k, fileURLToPath as O, isDev as z, getCodeWithWebComponent as I } from "code-inspector-core";
import h, { dirname as N } from "path";
function F(t, e) {
  return y(this, null, function* () {
    if (!t || !e)
      return [];
    const s = typeof t == "function" ? yield t() : t;
    let o = [];
    if (typeof s == "object" && !Array.isArray(s))
      for (const a in s) {
        const r = s[a], i = r.import || r;
        C(o, i, e);
      }
    else
      C(o, s, e);
    return o.filter((a) => !!a);
  });
}
function C(t, e, s) {
  typeof e == "string" ? t.push($(e, s)) : Array.isArray(e) && t.push(
    ...e.map((o) => $(o, s))
  );
}
function $(t, e) {
  return h.isAbsolute(t) ? k(t) : t.startsWith(".") ? h.resolve(e, k(t)) : "";
}
let l = "";
typeof __dirname != "undefined" ? l = __dirname : l = N(O(import.meta.url));
let A = !0;
const H = (t, e) => {
  var u, n, f;
  if (!A)
    return;
  A = !1;
  const s = (e == null ? void 0 : e.compiler) || e, o = (u = s == null ? void 0 : s.options) == null ? void 0 : u.module, a = (o == null ? void 0 : o.rules) || (o == null ? void 0 : o.loaders) || [];
  let r = t.include || [];
  Array.isArray(r) || (r = [r]);
  let i = t.exclude || [];
  Array.isArray(i) || (i = [i]), a.push(
    d({
      test: (n = t.match) != null ? n : /\.(vue|jsx|tsx|js|ts|mjs|mts)$/,
      exclude: [...i, /node_modules/],
      use: [
        {
          loader: h.resolve(l, "./loader.js"),
          options: t
        }
      ]
    }, t.enforcePre === !1 ? {} : { enforce: "pre" }),
    d({
      test: (f = t.match) != null ? f : /\.html$/,
      resourceQuery: /vue/,
      use: [
        {
          loader: h.resolve(l, "./loader.js"),
          options: t
        }
      ]
    }, t.enforcePre === !1 ? {} : { enforce: "pre" }),
    ...r.map((c) => d({
      resource: {
        and: [c, /\.(vue|jsx|tsx|js|ts|mjs|mts)$/]
      },
      use: [
        {
          loader: h.resolve(l, "./loader.js"),
          options: t
        }
      ]
    }, t.enforcePre === !1 ? {} : { enforce: "pre" })),
    j(d({}, t.injectTo ? { resource: t.injectTo } : {
      test: /\.(jsx|tsx|js|ts|mjs|mts)$/,
      exclude: /node_modules/
    }), {
      use: [
        {
          loader: h.resolve(l, "./inject-loader.js"),
          options: t
        }
      ],
      enforce: "post"
    })
  );
};
function g(t, e) {
  return I({
    options: j(d({}, t), { importClient: "code" }),
    file: "main.js",
    code: "",
    record: e,
    inject: !0
  });
}
function Q(o) {
  return y(this, arguments, function* ({
    options: t,
    record: e,
    assets: s
  }) {
    const a = Object.keys(s).filter((r) => /\.html$/.test(r));
    if (a.length) {
      const r = yield g(t, e);
      a.forEach((i) => {
        var n, f;
        const u = (f = (n = s[i]) == null ? void 0 : n.source) == null ? void 0 : f.call(n);
        if (typeof u == "string") {
          const c = u.replace(
            "<head>",
            '<head><script type="module">\n'.concat(r, "\n<\/script>")
          );
          s[i] = {
            source: () => c,
            size: () => c.length
          };
        }
      });
    }
  });
}
class V {
  constructor(e) {
    this.options = e;
  }
  apply(e) {
    return y(this, null, function* () {
      var o, a, r, i, u, n, f, c, x;
      if (A = !0, this.options.close || !z(
        this.options.dev,
        ((o = e == null ? void 0 : e.options) == null ? void 0 : o.mode) === "development" || process.env.NODE_ENV === "development"
      ))
        return;
      const s = {
        port: 0,
        entry: "",
        output: this.options.output,
        inputs: F(
          (a = e == null ? void 0 : e.options) == null ? void 0 : a.entry,
          (r = e == null ? void 0 : e.options) == null ? void 0 : r.context
        )
      };
      if (((u = (i = e == null ? void 0 : e.options) == null ? void 0 : i.cache) == null ? void 0 : u.type) === "filesystem" && (this.options.cache ? g(this.options, s) : e.options.cache.version = "code-inspector-".concat(Date.now())), ((c = (f = (n = e == null ? void 0 : e.options) == null ? void 0 : n.experiments) == null ? void 0 : f.cache) == null ? void 0 : c.type) === "persistent" && (this.options.cache ? g(this.options, s) : e.options.experiments.cache.version = "code-inspector-".concat(Date.now())), H(j(d({}, this.options), { record: s }), e), (x = e == null ? void 0 : e.hooks) != null && x.emit) {
        const _ = this.options;
        e.hooks.emit.tapAsync(
          "WebpackCodeInspectorPlugin",
          (p, w) => y(this, null, function* () {
            let v = {};
            p.getAssets ? v = yield p.getAssets() : v = p.assets, yield Q({
              options: _,
              record: s,
              assets: v
            }), w();
          })
        );
      }
    });
  }
}
export {
  V as default
};
