"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const workspace_1 = require("@schematics/angular/utility/workspace");
const file_utils_1 = require("../../utils/file-utils");
function default_1() {
    return (0, schematics_1.chain)([removeStylesFromConfig(), removeIconFonts(), removeFontStyles(), noticeAddSchematics()]);
}
function removeStylesFromConfig() {
    return (_, context) => __awaiter(this, void 0, void 0, function* () {
        return (0, workspace_1.updateWorkspace)((workspace) => {
            workspace.projects.forEach((project) => {
                project.targets.forEach((target) => {
                    var _a, _b;
                    const styles = (_a = target.options) === null || _a === void 0 ? void 0 : _a.styles;
                    if (!((_b = target.options) === null || _b === void 0 ? void 0 : _b.styles) || !styles || !(0, core_1.isJsonArray)(styles)) {
                        return;
                    }
                    const themes = [
                        'sap_horizon',
                        'sap_horizon_dark',
                        'sap_horizon_hcb',
                        'sap_horizon_hcw',
                        'sap_fiori_3',
                        'sap_fiori_3_dark',
                        'sap_fiori_3_hcb',
                        'sap_fiori_3_hcw',
                        'sap_fiori_3_light_dark'
                    ];
                    const stylesToRemove = [
                        'node_modules/fundamental-styles/dist/icon.css',
                        './node_modules/@sap-theming/theming-base-content/content/Base/baseLib/sap_fiori_3/css_variables.css',
                        './node_modules/fundamental-styles/dist/theming/sap_fiori_3.css',
                        ...themes.map((theme) => `./node_modules/@sap-theming/theming-base-content/content/Base/baseLib/${theme}/css_variables.css`),
                        ...themes.map((theme) => `./node_modules/fundamental-styles/dist/theming/${theme}.css`)
                    ];
                    stylesToRemove.forEach((styleToRemove) => {
                        const indexInStylesArray = styles.findIndex((configStyle) => typeof configStyle === 'string' && configStyle === styleToRemove);
                        if (indexInStylesArray > -1) {
                            styles.splice(indexInStylesArray, 1);
                            context.logger.info(`✅️ Removed '${styleToRemove}' style from angular.json`);
                        }
                    });
                    target.options.styles = styles;
                });
            });
        });
    });
}
function removeIconFonts() {
    return (tree, context) => (0, workspace_1.updateWorkspace)((workspace) => {
        workspace.projects.forEach((project) => {
            project.targets.forEach((target) => {
                var _a, _b;
                const styles = (_a = target.options) === null || _a === void 0 ? void 0 : _a.styles;
                if (!((_b = target.options) === null || _b === void 0 ? void 0 : _b.styles) || !styles || !(0, core_1.isJsonArray)(styles)) {
                    return;
                }
                const iconFonts = ['sap_fiori_3_fonts', 'sap_horizon_fonts'];
                iconFonts.forEach((fontFile) => {
                    // Remove file
                    const fontFileWithPath = `${project.sourceRoot}/theming/${fontFile}.css`;
                    if (tree.exists(fontFileWithPath)) {
                        tree.delete(fontFileWithPath);
                        context.logger.info(`✅️ Removed ${fontFile}.css file.`);
                    }
                    // Remove from config
                    const indexInStylesArray = styles.findIndex((style) => (0, core_1.isJsonObject)(style) && (style === null || style === void 0 ? void 0 : style.bundleName) === `${fontFile}`);
                    if (indexInStylesArray > -1) {
                        styles.splice(indexInStylesArray, 1);
                        context.logger.info(`✅️ Removed '${fontFile}' style from angular.json`);
                    }
                });
                target.options.styles = styles;
            });
        });
    });
}
function removeFontStyles() {
    return (tree, context) => {
        const styleSheets = (0, file_utils_1.findStylesheetFiles)(tree);
        if (styleSheets.length > 0) {
            const FONT_FACE_REGEX = /(@font-face)[\w\s]?{[s\S\W\w]+?(?=\s}\s)?\s?}[\n\s]*/gi;
            styleSheets.forEach((styleSheet) => {
                const buffer = tree.read(styleSheet);
                if (!buffer) {
                    return;
                }
                let contents = buffer.toString();
                if (FONT_FACE_REGEX.test(contents)) {
                    const matches = contents
                        .match(FONT_FACE_REGEX)
                        .filter((m) => { var _a; return (_a = m.match(/~@sap-theming.*\.woff/)) === null || _a === void 0 ? void 0 : _a.length; });
                    if (matches.length === 0) {
                        return;
                    }
                    matches.forEach((match) => {
                        contents = contents.replace(match, '');
                    });
                    tree.overwrite(styleSheet, contents);
                    context.logger.info(`✅️ Removed font styles from stylesheets.`);
                }
            });
        }
        context.logger.info(`⚠️ Notice: If you have imported font styles from @sap-theming, please remove it, since now it is controlled by fundamental-ngx`);
        return tree;
    };
}
function noticeAddSchematics() {
    return (_, context) => {
        // Unfortunately we cannot run it on our own because ng-update doesn't respect schema so there is no way to get options and pass them further
        context.logger.info(`ℹ️ Now you have to run ng-add schematics once again to set up things in the right way.`);
    };
}
//# sourceMappingURL=index.js.map