"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const utility_1 = require("../../lib/utility");
const sharp_1 = __importDefault(require("sharp"));
const angular_utility_1 = require("../../lib/angular-utility");
const lib_1 = require("../../lib");
const ionic_1 = require("../../lib/ionic");
const SPLASH_WIDTH = 2732;
const SPLASH_HEIGHT = 2732;
const LOGO_WIDTH = 1024;
const LOGO_HEIGHT = 1024;
function checkImageSize(logger, img, width, height) {
    return __awaiter(this, void 0, void 0, function* () {
        let metadata = yield img.metadata();
        if (metadata.width && metadata.width < width) {
            logger.warn(`Image width is too small: ${metadata.width}px. It should be at least ${width}px`);
        }
        if (metadata.height && metadata.height < width) {
            logger.warn(`Image height is too small: ${metadata.height}px. It should be at least ${height}px`);
        }
    });
}
/**
 * See ionic documentation about splash and icon {@link https://ionicframework.com/docs/cli/commands/cordova-resources}
 *      The source image for icons should ideally be at least 1024×1024px and located at resources/icon.png.
 *      The source image for splash screens should ideally be at least 2732×2732px and located at resources/splash.png.
 *      If you used ionic start, there should already be default Ionic resources in the resources/ directory, which you can overwrite.
 *
 */
function default_1(_options) {
    return schematics_1.chain([
        angular_utility_1.toPromiseRule((tree, _context) => __awaiter(this, void 0, void 0, function* () {
            if (_options.splash || _options.logo) {
                let project = lib_1.loadProject(tree, _context);
                if (_options.splash) {
                    let splashBuffer = utility_1.getFileContentFromPath(_options.splash);
                    let splash = sharp_1.default(splashBuffer).removeAlpha();
                    yield checkImageSize(_context.logger, splash, SPLASH_WIDTH, SPLASH_HEIGHT);
                    splash.resize(SPLASH_WIDTH, SPLASH_HEIGHT);
                    project.appConfigSchema.app.splashScreen = {
                        image: _options.logo
                    };
                    project.createOrOverwrite('resources/splash.png', yield splash.toBuffer(), true);
                }
                if (_options.logo) {
                    let logoSharp = yield sharp_1.default(utility_1.getFileContentFromPath(_options.logo));
                    yield checkImageSize(_context.logger, logoSharp, LOGO_WIDTH, LOGO_HEIGHT);
                    logoSharp.resize(LOGO_WIDTH, LOGO_HEIGHT);
                    let logoBuffer = yield logoSharp.toBuffer();
                    let logo48x48 = yield sharp_1.default(logoBuffer).resize(48, 48).toBuffer();
                    let faviconBuffer = _options.favicon ? utility_1.getFileContentFromPath(_options.favicon) : logoBuffer;
                    faviconBuffer = yield sharp_1.default(faviconBuffer).resize(260, 260).toBuffer();
                    project.appConfigSchema.app.logo = {
                        image: _options.logo
                    };
                    project.createOrOverwrite(ionic_1.IONIC_RESOURCE_ICON, logoBuffer, _options.overwrite);
                    project.createOrOverwrite('src/assets/icon/favicon.png', faviconBuffer, _options.overwrite);
                    project.createOrOverwrite('src/assets/icons/logo-48x48.png', logo48x48, _options.overwrite);
                }
                project.save();
            }
        })),
        _options.generate ? utility_1.shellCommandSchematic({
            command: 'ionic',
            args: [
                'cordova',
                'resources'
            ]
        }) : schematics_1.noop()
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map