"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/webpack-plugin.ts
var webpack_plugin_exports = {};
__export(webpack_plugin_exports, {
  VisualEditsWebpackPlugin: () => VisualEditsWebpackPlugin,
  default: () => webpack_plugin_default
});
module.exports = __toCommonJS(webpack_plugin_exports);
var import_path = __toESM(require("path"));
var import_fs = __toESM(require("fs"));
var VisualEditsWebpackPlugin = class {
  constructor(options = {}) {
    this.options = options;
  }
  apply(compiler) {
    const monitorPath = import_path.default.resolve(__dirname, "../dist/visual-edit-overlay.js");
    const tailwindCdn = this.options.tailwindCdn ?? "https://cdn.tailwindcss.com";
    compiler.hooks.emit.tapAsync(
      "VisualEditsPlugin",
      (compilation, cb) => {
        try {
          const source = import_fs.default.readFileSync(monitorPath, "utf-8");
          compilation.assets["visual-edit-overlay.js"] = {
            source: () => source,
            size: () => source.length
          };
        } catch (err) {
          console.error(
            `[VisualEditsPlugin] Failed to read overlay: ${err.message}`
          );
        }
        cb();
      }
    );
    compiler.hooks.compilation.tap("VisualEditsPlugin", (compilation) => {
      try {
        const htmlPluginPath = require.resolve("html-webpack-plugin", {
          paths: [compiler.context || process.cwd()]
        });
        const HtmlWebpackPlugin = require(htmlPluginPath);
        HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
          "VisualEditsPlugin",
          (data, cb) => {
            const tailwindSnippet = tailwindCdn ? `window.tailwind=window.tailwind||{};tailwind.config={corePlugins:{preflight:false}};var t=document.createElement("script");t.src="${tailwindCdn}";document.head.appendChild(t);` : "";
            const snippet = `<script>
if(window.self!==window.top){
  var s=document.createElement("script");s.src="/visual-edit-overlay.js";document.head.appendChild(s);
  ${tailwindSnippet}
}
</script>`;
            data.html = data.html.replace("</head>", snippet + "\n</head>");
            cb(null, data);
          }
        );
      } catch {
      }
    });
  }
};
var webpack_plugin_default = VisualEditsWebpackPlugin;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  VisualEditsWebpackPlugin
});
