"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.myCompileNgcTransformFactory = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const transform_1 = require("ng-packagr/lib/graph/transform");
const nodes_1 = require("ng-packagr/lib/ng-package/nodes");
const tsconfig_1 = require("ng-packagr/lib/ts/tsconfig");
const ora_1 = __importDefault(require("ora"));
const path = __importStar(require("path"));
const typescript_1 = __importDefault(require("typescript"));
const compile_source_files_1 = require("./compile-source-files");
const myCompileNgcTransformFactory = (StylesheetProcessor, options) => {
    return (0, transform_1.transformFromPromise)((graph) => __awaiter(void 0, void 0, void 0, function* () {
        var _a, _b;
        var _c;
        const spinner = (0, ora_1.default)({
            hideCursor: false,
            discardStdin: false,
        });
        const entryPoints = graph.filter(nodes_1.isEntryPoint);
        const entryPoint = entryPoints.find((0, nodes_1.isEntryPointInProgress)());
        const ngPackageNode = graph.find(nodes_1.isPackage);
        const projectBasePath = ngPackageNode.data.primary.basePath;
        try {
            // Add paths mappings for dependencies
            const tsConfig = (0, tsconfig_1.setDependenciesTsConfigPaths)(entryPoint.data.tsConfig, entryPoints);
            // Compile TypeScript sources
            const { esm2022: esm2022, declarations } = entryPoint.data.destinationFiles;
            const { basePath, cssUrl, styleIncludePaths } = entryPoint.data.entryPoint;
            const { moduleResolutionCache } = entryPoint.cache;
            spinner.start(`Compiling with Angular sources in Ivy ${tsConfig.options.compilationMode || 'full'} compilation mode.`);
            (_a = (_c = entryPoint.cache).stylesheetProcessor) !== null && _a !== void 0 ? _a : (_c.stylesheetProcessor = new StylesheetProcessor(projectBasePath, basePath, cssUrl, styleIncludePaths, options.cacheEnabled && options.cacheDirectory));
            yield (0, compile_source_files_1.compileSourceFiles)(graph, tsConfig, moduleResolutionCache, {
                outDir: path.dirname(esm2022),
                declarationDir: path.dirname(declarations),
                declaration: true,
                target: typescript_1.default.ScriptTarget.ES2022,
            }, entryPoint.cache.stylesheetProcessor, options.watch);
        }
        catch (error) {
            spinner.fail();
            throw error;
        }
        finally {
            if (!options.watch) {
                (_b = entryPoint.cache.stylesheetProcessor) === null || _b === void 0 ? void 0 : _b.destroy();
            }
        }
        spinner.succeed();
        return graph;
    }));
};
exports.myCompileNgcTransformFactory = myCompileNgcTransformFactory;
