"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);
var HtmlAppIconPlugin_exports = {};
__export(HtmlAppIconPlugin_exports, {
  HtmlAppIconPlugin: () => HtmlAppIconPlugin
});
module.exports = __toCommonJS(HtmlAppIconPlugin_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_webpack_sources = __toESM(require("../../compiled/webpack-sources"));
var import_util = require("./util");
class HtmlAppIconPlugin {
  constructor(options) {
    this.name = "HtmlAppIconPlugin";
    this.distDir = options.distDir;
    this.iconPath = options.iconPath;
    this.HtmlPlugin = options.HtmlPlugin;
  }
  apply(compiler) {
    if (!import_fs.default.existsSync(this.iconPath)) {
      throw new Error(
        `[${this.name}] Can not find the app icon, please check if the '${this.iconPath}' file exists'.`
      );
    }
    const iconRelativePath = import_path.default.join(
      this.distDir,
      import_path.default.basename(this.iconPath)
    );
    compiler.hooks.compilation.tap(this.name, (compilation) => {
      this.HtmlPlugin.getHooks(compilation).alterAssetTagGroups.tap(
        this.name,
        (data) => {
          const { publicPath } = compiler.options.output;
          data.headTags.unshift({
            tagName: "link",
            voidTag: true,
            attributes: {
              rel: "apple-touch-icon",
              sizes: "180*180",
              href: `${publicPath}${iconRelativePath}`
            },
            meta: {}
          });
          return data;
        }
      );
    });
    compiler.hooks.thisCompilation.tap(
      this.name,
      (compilation) => {
        compilation.hooks.processAssets.tap(
          {
            name: this.name,
            stage: import_util.COMPILATION_PROCESS_STAGE.PROCESS_ASSETS_STAGE_PRE_PROCESS
          },
          (assets) => {
            const source = import_fs.default.readFileSync(this.iconPath);
            assets[iconRelativePath] = new import_webpack_sources.default.RawSource(
              source,
              false
            );
          }
        );
      }
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  HtmlAppIconPlugin
});
