"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const lib_1 = require("../../lib");
const angular_utility_1 = require("../../lib/angular-utility");
const ionic_1 = require("../../lib/ionic");
const defintions_1 = require("../../lib/pwa/defintions");
const utility_1 = require("../../lib/utility");
function default_1(_options) {
    return schematics_1.chain([
        angular_utility_1.toPromiseRule(async (tree, _context) => {
            const project = lib_1.loadProject(tree, _context);
            const appConfig = project.appConfigSchema;
            // TODO check if PWA is enabled in config ? 
            const pwaOptions = {
                title: appConfig.app.name,
                project: _options.project || lib_1.getDefaultAngularProject(tree),
            };
            const pwa = project.pwa;
            if (_options.overwrite) {
                _context.logger.warn('Overwriting PWA config and icons...');
                const files = [
                    ...defintions_1.PWA_MANIFEST_CANDIDATES,
                    defintions_1.PWA_NGSW_CONFIG
                ];
                if (pwa.manifest) {
                    const iconFiles = pwa.manifest.icons.map(icon => pwa.iconPath(icon));
                    files.push(...iconFiles);
                }
                _context.logger.warn(files.map(f => `\t- ${f}`).join('\n'));
                for (const file of files) {
                    if (tree.exists(file)) {
                        tree.delete(file);
                    }
                }
            }
            // console.log('Options: ', pwaOptions);
            if (_options.skipInstall) {
                _context.logger.warn(`Cannot add @angular/pwa package as "skipInstall" option has been given`);
                _context.logger.info(`Run "ng add @angular/pwa" command manually`);
                return schematics_1.noop();
            }
            else {
                return utility_1.externalPrivateSchematic('@angular/pwa', 'ng-add', pwaOptions);
            }
        }),
        angular_utility_1.toPromiseRule(async (tree, context) => {
            const project = lib_1.loadProject(tree, context);
            const pwa = project.pwa;
            if (!tree.exists(ionic_1.IONIC_RESOURCE_ICON)) {
                context.logger.warn(`App icon "${ionic_1.IONIC_RESOURCE_ICON}" is not set yet. cannot generate icons for progressive app`);
                return;
            }
            if (!pwa.manifest) {
                context.logger.warn(`Manifest file "${defintions_1.PWA_MANIFEST}" is not set yet. cannot generate icons for progressive app`);
                return;
            }
            let sharp;
            try {
                sharp = (await Promise.resolve().then(() => __importStar(require('sharp')))).default;
            }
            catch (err) {
                context.logger.warn(`Image processing library 'sharp' is not properly installed. Cannot generate icons.`);
                return;
            }
            for (const icon of pwa.manifest.icons) {
                context.logger.info(`Generating icon ${pwa.iconPath(icon)} from template: ${ionic_1.IONIC_RESOURCE_ICON}`);
                const iconFileTemplate = tree.read(ionic_1.IONIC_RESOURCE_ICON);
                if (iconFileTemplate) {
                    try {
                        const imgTemplate = sharp(iconFileTemplate);
                        const [width, height] = icon.sizes.split('x');
                        const outputImage = await imgTemplate.resize(parseInt(width, 10), parseInt(height, 10)).toBuffer();
                        tree.overwrite(pwa.iconPath(icon), outputImage);
                    }
                    catch (err) {
                        context.logger.warn(`Cannot generate icon  ${pwa.iconPath(icon)}: ${err}`);
                    }
                }
                else {
                    context.logger.warn(`App icon not found in ${ionic_1.IONIC_RESOURCE_ICON}`);
                }
            }
        })
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map