"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.addStyles = addStyles;
const schematics_1 = require("@angular-devkit/schematics");
const workspace_1 = require("../utils/workspace");
/**
 * Add styles to angular.json
 * @param options
 */
function addStyles(options) {
    return (0, schematics_1.chain)([addStylesToConfig(options), addAssetsToConfig(options)]);
}
function handleError(context, error, message) {
    context.logger.error(message);
    throw new schematics_1.SchematicsException(`${message}: ${error.message}`);
}
function addStylesToConfig(options) {
    return (tree, context) => __awaiter(this, void 0, void 0, function* () {
        var _a;
        try {
            const additionalStyle = './node_modules/@fundamental-ngx/core/styles/fundamental-ngx-core.css';
            const buildTarget = yield (0, workspace_1.getProjectBuildTarget)(tree, options.project);
            const stylesArray = ((_a = buildTarget.options) === null || _a === void 0 ? void 0 : _a.styles) || [];
            if (!stylesArray.includes(additionalStyle)) {
                stylesArray.push(additionalStyle);
                context.logger.info(`✅️ Added style to angular.json.`);
            }
            else {
                context.logger.info(`✅️ Style already exists in angular.json. Skipping.`);
            }
            buildTarget.options.styles = stylesArray;
            const workspace = yield (0, workspace_1.getWorkspaceDefinition)(tree);
            yield (0, workspace_1.updateWorkspaceDefinition)(tree, workspace);
        }
        catch (error) {
            handleError(context, error, 'Failed to add styles configuration');
        }
    });
}
function addAssetsToConfig(options) {
    return (tree, context) => __awaiter(this, void 0, void 0, function* () {
        var _a;
        try {
            const additionalAssets = [
                {
                    glob: '**/css_variables.css',
                    input: './node_modules/@sap-theming/theming-base-content/content/Base/baseLib/',
                    output: './assets/theming-base/'
                },
                {
                    glob: '**/*',
                    input: './node_modules/@sap-theming/theming-base-content/content/Base/baseLib/baseTheme/fonts/',
                    output: './assets/theming-base/baseTheme/fonts/'
                },
                {
                    glob: '**/*',
                    input: './node_modules/@sap-theming/theming-base-content/content/Base/baseLib/sap_horizon/fonts/',
                    output: './assets/theming-base/sap_horizon/fonts/'
                },
                {
                    glob: '**/*',
                    input: './node_modules/fundamental-styles/dist/theming/',
                    output: './assets/fundamental-styles-theming/'
                }
            ];
            const buildTarget = yield (0, workspace_1.getProjectBuildTarget)(tree, options.project);
            const assetsArray = ((_a = buildTarget.options) === null || _a === void 0 ? void 0 : _a.assets) || [];
            const newAssets = additionalAssets.filter((newAsset) => !assetsArray.some((existingAsset) => typeof existingAsset === 'object' &&
                existingAsset.input === newAsset.input &&
                existingAsset.glob === newAsset.glob &&
                existingAsset.output === newAsset.output));
            if (newAssets.length > 0) {
                assetsArray.push(...newAssets);
                context.logger.info(`✅️ Added assets to angular.json.`);
            }
            else {
                context.logger.info(`✅️ Assets already exist in angular.json. Skipping.`);
            }
            buildTarget.options.assets = assetsArray;
            const workspace = yield (0, workspace_1.getWorkspaceDefinition)(tree);
            yield (0, workspace_1.updateWorkspaceDefinition)(tree, workspace);
        }
        catch (error) {
            handleError(context, error, 'Failed to add assets configuration');
        }
    });
}
//# sourceMappingURL=index.js.map