"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const utility_1 = require("@schematics/angular/utility");
const helper_1 = require("../helper/helper");
function updateAppConfig(tree, options) {
    const filePath = `${options.sourceRoot}/app/app.config.ts`;
    if (!tree.exists(filePath)) {
        throw new Error(`${filePath} not found`);
    }
    const fileContent = tree.read(filePath)?.toString('utf-8');
    if (!fileContent) {
        throw new Error(`Failed to read ${filePath}`);
    }
    let updatedContent = fileContent;
    // Add import if not already present
    if (!fileContent.includes("import { provideMediaViewer } from '@yuuvis/media-viewer';")) {
        updatedContent = `import { provideMediaViewer } from '@yuuvis/media-viewer';\n` + updatedContent;
    }
    // Add import if not already present
    if (!fileContent.includes("import { environment } from '../environments/environment.prod';")) {
        updatedContent = `import { environment } from '../environments/environment.prod';\n` + updatedContent;
    }
    // Modify providers array
    const providersRegex = /providers:\s*\[([^\]]*)\]/s;
    const match = fileContent.match(providersRegex);
    if (match) {
        const providersArray = match[1].trim();
        if (!providersArray.includes(`provideMediaViewer(`)) {
            updatedContent = updatedContent.replace(providersRegex, `providers: [$1, provideMediaViewer(environment, { office: { defaultHost: '${options.officeEndpoint}' }})]`);
        }
    }
    tree.overwrite(filePath, updatedContent);
    return tree;
}
function updateAngularJson(tree, options) {
    const angularJsonPath = `angular.json`;
    if (!tree.exists(angularJsonPath)) {
        throw new Error(`${angularJsonPath} not found`);
    }
    const angularJsonContent = tree.read(angularJsonPath)?.toString('utf-8');
    if (!angularJsonContent) {
        throw new Error(`Failed to read ${angularJsonPath}`);
    }
    const angularJson = JSON.parse(angularJsonContent);
    const project = options.project || Object.keys(angularJson.projects)[0];
    if (!angularJson.projects[project]) {
        throw new Error(`Project ${project} not found in angular.json`);
    }
    const buildOptions = angularJson.projects[project].architect.build.options;
    buildOptions.assets = buildOptions.assets || [];
    buildOptions.assets.push({
        glob: '**/*',
        input: 'node_modules/monaco-editor',
        output: '/assets/monaco/'
    });
    buildOptions.assets.push({
        glob: '**/*',
        input: 'node_modules/ngx-extended-pdf-viewer/assets/',
        output: '/assets/'
    });
    if (!options.skipEinvoice) {
        buildOptions.assets.push({
            glob: '**/*',
            input: 'node_modules/@yuuvis/media-viewer/lib/assets/xslt',
            output: '/assets/xslt'
        });
        buildOptions.scripts = buildOptions.scripts || [];
        if (!buildOptions.scripts.includes('node_modules/@yuuvis/media-viewer/lib/assets/saxon-js/SaxonJS3.rt.js')) {
            buildOptions.scripts.push('node_modules/@yuuvis/media-viewer/lib/assets/saxon-js/SaxonJS3.rt.js');
        }
    }
    tree.overwrite(angularJsonPath, JSON.stringify(angularJson, null, 2));
    return tree;
}
function updateStyles(tree, options) {
    const stylesPath = `${options.sourceRoot}/styles.scss`;
    if (!tree.exists(stylesPath)) {
        throw new Error(`${stylesPath} not found`);
    }
    const stylesContent = tree.read(stylesPath)?.toString('utf-8') || '';
    const importStatement = `@use '@yuuvis/media-viewer/scss';`;
    let updatedStylesContent = stylesContent;
    if (!stylesContent.includes(importStatement)) {
        updatedStylesContent = `${importStatement}\n${stylesContent}`;
    }
    tree.overwrite(stylesPath, updatedStylesContent);
    return tree;
}
exports.default = (options) => {
    return async (tree, ctx) => {
        if (tree.exists('package.json')) {
            const json = (0, helper_1.readSourceFile)(tree, 'package.json');
            const versionNumber = (0, helper_1.getVersion)(json);
            if (versionNumber < 15) {
                throw new schematics_1.SchematicsException('This Angular version is not Supported. Please refer to @yuuvis/project');
            }
            //  else if (versionNumber > 19) {
            //   throw new SchematicsException('This Angular version is not yet Supported. We are working on it realy Hard!');
            // }
        }
        else {
            throw new schematics_1.SchematicsException("We coudn't find the package JSON");
        }
        const workspace = await (0, utility_1.readWorkspace)(tree);
        const project = workspace.projects.get(options.project);
        options.sourceRoot = project?.sourceRoot;
        options.root = project?.root;
        return (0, schematics_1.chain)([
            () => (tree) => updateAppConfig(tree, options),
            () => (tree) => updateAngularJson(tree, options),
            () => (tree) => updateStyles(tree, options)
        ]);
    };
};
//# sourceMappingURL=index.js.map