"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 utility_1 = require("../../lib/utility");
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;
async function checkImageSize(logger, img, width, height) {
    const metadata = await 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(async (tree, _context) => {
            const project = lib_1.loadProject(tree, _context);
            let splashFilepath = _options.splash;
            let logoFilepath = _options.logo;
            if ((_options.overwrite || _options.generate)) {
                if (!splashFilepath) {
                    splashFilepath = (project.appConfigSchema.app.splashScreen || {}).image;
                }
                if (!logoFilepath) {
                    logoFilepath = (project.appConfigSchema.app.logo || {}).image;
                }
            }
            if (splashFilepath || logoFilepath) {
                const sharp = (await Promise.resolve().then(() => __importStar(require("sharp")))).default;
                if (splashFilepath) {
                    const splashBuffer = utility_1.getFileContentFromPath(splashFilepath);
                    const splash = sharp(splashBuffer).removeAlpha();
                    await checkImageSize(_context.logger, splash, SPLASH_WIDTH, SPLASH_HEIGHT); // TODO check type
                    splash.resize(SPLASH_WIDTH, SPLASH_HEIGHT);
                    project.appConfigSchema.app.splashScreen = {
                        image: splashFilepath
                    };
                    project.createOrOverwrite('resources/splash.png', await splash.toBuffer(), true);
                }
                if (logoFilepath) {
                    const logoSharp = await sharp(utility_1.getFileContentFromPath(logoFilepath));
                    await checkImageSize(_context.logger, logoSharp, LOGO_WIDTH, LOGO_HEIGHT); // TODO check type
                    logoSharp.resize(LOGO_WIDTH, LOGO_HEIGHT);
                    const logoBuffer = await logoSharp.toBuffer();
                    const logo48x48 = await sharp(logoBuffer).resize(48, 48).toBuffer();
                    let faviconBuffer = _options.favicon ? utility_1.getFileContentFromPath(_options.favicon) : logoBuffer;
                    faviconBuffer = await sharp(faviconBuffer).resize(260, 260).toBuffer();
                    project.appConfigSchema.app.logo = {
                        image: logoFilepath
                    };
                    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