"use strict";
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 cordova_1 = require("../../lib/cordova");
const xml_editor_1 = require("../../lib/xml-editor");
const utility_1 = require("./utility");
function default_1(_options) {
    return schematics_1.chain([
        (tree, _context) => {
            if (!tree.exists('config.xml') || _options.overwrite) {
                const project = lib_1.loadProject(tree, _context);
                const enableNFCTapDeviceDiscoveryPreferenceValue = project.protocolOptions('nfc').energyHarvesting ? 'false' : 'true';
                return angular_utility_1.applyWithOverwriteRule(schematics_1.url('./files'), [
                    schematics_1.template(Object.assign(Object.assign({}, project.toTemplate()), { nfcHost: project.appConfigSchema.app.domain, enableNFCTapDeviceDiscoveryPreferenceValue }))
                ]);
            }
            else {
                return schematics_1.noop();
            }
        },
        (tree, _context) => {
            const project = lib_1.loadProject(tree, _context);
            const enableNFCTapDeviceDiscoveryPreferenceValue = project.protocolOptions('nfc').energyHarvesting ? 'false' : 'true';
            const appConfig = project.appConfigSchema;
            const appConfigBuilder = project.appConfigBuilder;
            cordova_1.updateCordovaConfig(tree, (editor) => {
                if (!appConfig.app.id) {
                    throw new lib_1.MissingAppConfigKey(`app.id`);
                }
                if (!appConfig.app.name) {
                    throw new lib_1.MissingAppConfigKey(`app.name`);
                }
                editor.set('widget.name', appConfig.app.name);
                editor.set('widget.description', appConfig.app.description || 'An awesome Ionic/Cordova app to communicate with iotize devices');
                editor.set('widget.#.id', appConfig.app.id);
                editor.set('widget.#.version', appConfig.app.version);
                const androidVersionCode = appConfigBuilder.androidOptions.versionCode || utility_1.computeVersionCodeFromVersion(appConfig.app.version);
                editor.set('widget.#.android-versionCode', androidVersionCode);
                // editor.set('widget.preference.#.id', appConfig.app.id);
                xml_editor_1.setPreferenceValue(editor, 'EnableNFCTapDeviceDiscovery', enableNFCTapDeviceDiscoveryPreferenceValue);
            });
            // Don't need this as ionic will automatically add missing plugins
            // await addCordovaPlugins(tree, appConfig);
            // updateAppConfig(tree, (data: TapAppGeneratorSchema) => {
            //     data.app.integrations = data.app.platforms.browser || {};
            //     data.app.integrations!.cordova = true;
            // });
        },
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map