"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DynamicLibraryComponentEntryPlugin = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const core_1 = require("@angular-devkit/core");
const path_1 = __importDefault(require("path"));
const static_injector_1 = require("static-injector");
const webpack = __importStar(require("webpack"));
const library_1 = require("../../library");
const const_1 = require("../const");
const i0 = __importStar(require("static-injector"));
const i1 = __importStar(require("../../platform/platform"));
const CUSTOM_URI = 'dynamic';
const CUSTOM_URI_REG = /^dynamic:\/\/__license(?:\/|\\)(.*)\.ts$/;
class DynamicLibraryComponentEntryPlugin {
    constructor(buildPlatform) {
        this.buildPlatform = buildPlatform;
        this.libraryComponentMap = new Map();
    }
    apply(compiler) {
        compiler.hooks.thisCompilation.tap('DynamicLibraryEntryPlugin', (thisCompilation) => {
            thisCompilation[const_1.LibrarySymbol] = thisCompilation[const_1.LibrarySymbol] || { buildPlatform: this.buildPlatform };
            const hooks = webpack.NormalModule.getCompilationHooks(thisCompilation);
            hooks.readResource
                .for(CUSTOM_URI)
                .tapAsync('DynamicLibraryEntryPlugin', (loaderContext, callback) => {
                const resourcePath = loaderContext.resourcePath;
                const id = resourcePath.match(CUSTOM_URI_REG)[1];
                const libraryMeta = this.libraryComponentMap.get(id);
                callback(undefined, `
            import * as amp from 'angular-miniprogram';
            import * as library from '${libraryMeta === null || libraryMeta === void 0 ? void 0 : libraryMeta.contextPath}';
            amp.componentRegistry(library.${libraryMeta === null || libraryMeta === void 0 ? void 0 : libraryMeta.className});
            `);
                return;
            });
            compiler.hooks.finishMake.tapAsync('DynamicLibraryEntryPlugin', (compilation, callback) => {
                const libraryLoaderContext = compilation[const_1.LibrarySymbol];
                if (compilation !== thisCompilation) {
                    callback(undefined);
                    return;
                }
                if (libraryLoaderContext.libraryMetaList) {
                    libraryLoaderContext.libraryMetaList.forEach((item) => {
                        this.libraryComponentMap.set(item.id, item);
                    });
                }
                if (this.libraryComponentMap.size === 0) {
                    callback(undefined);
                    return;
                }
                let j = 0;
                this.libraryComponentMap.forEach((meta) => {
                    const entry = (0, core_1.join)((0, core_1.normalize)(library_1.LIBRARY_OUTPUT_ROOTDIR), meta.libraryPath);
                    const dep = webpack.EntryPlugin.createDependency(`${CUSTOM_URI}://${path_1.default.join('__license', meta.id)}.ts`, entry);
                    compilation.addEntry(compiler.context, dep, entry, (err, result) => {
                        j++;
                        if (j === this.libraryComponentMap.size) {
                            callback(undefined);
                        }
                    });
                });
            });
        });
    }
}
exports.DynamicLibraryComponentEntryPlugin = DynamicLibraryComponentEntryPlugin;
DynamicLibraryComponentEntryPlugin.ɵfac = function DynamicLibraryComponentEntryPlugin_Factory(t) { return new (t || DynamicLibraryComponentEntryPlugin)(i0.ɵɵinject(i1.BuildPlatform)); };
DynamicLibraryComponentEntryPlugin.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: DynamicLibraryComponentEntryPlugin, factory: DynamicLibraryComponentEntryPlugin.ɵfac });
