"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const utility_1 = require("../../lib/utility");
const lib_1 = require("../../lib");
const defintions_1 = require("../../lib/pwa/defintions");
const sharp = require("sharp");
const angular_utility_1 = require("../../lib/angular-utility");
const ionic_1 = require("../../lib/ionic");
// const PWA_ICONS = [
//     '/src/assets/icons/icon-128x128.png',
//     '/src/assets/icons/icon-144x144.png',
//     '/src/assets/icons/icon-152x152.png',
//     '/src/assets/icons/icon-192x192.png',
//     '/src/assets/icons/icon-384x384.png',
//     '/src/assets/icons/icon-512x512.png',
//     '/src/assets/icons/icon-72x72.png',
//     '/src/assets/icons/icon-96x96.png',
// ]
function default_1(_options) {
    return schematics_1.chain([
        (tree, _context) => {
            let project = lib_1.loadProject(tree, _context);
            let appConfig = project.appConfigSchema;
            // TODO check if PWA is enabled in config ? 
            let pwaOptions = {
                title: appConfig.app.name,
                project: _options.project || lib_1.getDefaultAngularProject(tree),
            };
            let pwa = project.pwa;
            if (_options.overwrite) {
                _context.logger.warn('Overwriting PWA config and icons...');
                let files = [
                    defintions_1.PWA_MANIFEST,
                    defintions_1.PWA_NGSW_CONFIG
                ];
                if (pwa.manifest) {
                    let iconFiles = pwa.manifest.icons.map(icon => pwa.iconPath(icon));
                    files.push(...iconFiles);
                }
                for (let file of files) {
                    if (tree.exists(file)) {
                        tree.delete(file);
                    }
                }
            }
            // console.log('Options: ', pwaOptions);
            return utility_1.externalPrivateSchematic('@angular/pwa', 'ng-add', pwaOptions);
        },
        angular_utility_1.toPromiseRule((tree, _context) => __awaiter(this, void 0, void 0, function* () {
            let project = lib_1.loadProject(tree, _context);
            let 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;
            }
            for (let icon of pwa.manifest.icons) {
                _context.logger.info(`Generating icon ${pwa.iconPath(icon)} from template: ${ionic_1.IONIC_RESOURCE_ICON}`);
                let iconFileTemplate = utility_1.getFileContentFromPath(ionic_1.IONIC_RESOURCE_ICON);
                try {
                    let imgTemplate = sharp(iconFileTemplate);
                    let [width, height] = icon.sizes.split('x');
                    let outputImage = yield imgTemplate.resize(parseInt(width), parseInt(height)).toBuffer();
                    tree.overwrite(pwa.iconPath(icon), outputImage);
                }
                catch (err) {
                    _context.logger.warn(`Cannot generate icon  ${pwa.iconPath(icon)}: ${err}`);
                }
            }
        }))
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map