"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const strings_1 = require("@angular-devkit/core/src/utils/strings");
const schematics_1 = require("@angular-devkit/schematics");
const lib_1 = require("../../lib");
const angular_utility_1 = require("../../lib/angular-utility");
const definitions_1 = require("../../lib/definitions");
const protocolConfig = {};
protocolConfig[definitions_1.AppProtocol.BLE] = {
    icon: 'bluetooth'
};
protocolConfig[definitions_1.AppProtocol.MQTT] = {
    icon: 'cloud'
};
protocolConfig[definitions_1.AppProtocol.SOCKET] = {
    icon: 'wifi'
};
// protocolConfig[Protocol.WEBSOCKET] ={
//     icon: 'wifi'
// }
protocolConfig[definitions_1.AppProtocol.NFC] = {
    src: 'assets/svg/nfc.svg'
};
function getIconFromProtocol(protocol) {
    if (!protocolConfig[protocol]) {
        return undefined;
    }
    return protocolConfig[protocol].icon;
}
function getIconSrcFromProtocol(protocol) {
    if (!protocolConfig[protocol]) {
        return undefined;
    }
    return protocolConfig[protocol].src;
}
function createTemplateRuleFor(name, ws, path, _options) {
    let sourceTemplates = schematics_1.url(`files-${name}`);
    return angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
        schematics_1.template(Object.assign({}, ws.toTemplate())),
        schematics_1.move(path)
    ], {
        overwrite: _options.overwrite
    });
}
function default_1(_options) {
    return schematics_1.chain([
        (tree, _context) => {
            let project = lib_1.loadProject(tree, _context);
            let relativePath = _options.name || 'pages/tap-connect';
            let page = project.appConfigBuilder.connectionPage(relativePath);
            if (page.enabled) {
                const { name, path } = lib_1.getAngularProjectPath(relativePath, tree, _options);
                let protocols = project.communicationProtocols;
                if (protocols.length == 0) {
                    throw new schematics_1.SchematicsException(`At least one communication protocol is required to generate Tap connect module`);
                }
                let defaultProtocol = `${strings_1.dasherize(protocols[0].toLowerCase())}`;
                let sourceTemplates = schematics_1.url('files');
                let rules = [];
                if (page.protocols.ble && page.protocols.ble.enabled) {
                    rules.push(createTemplateRuleFor('ble', project, path, _options));
                }
                if (page.protocols.socket && page.protocols.socket.enabled) {
                    rules.push(createTemplateRuleFor('socket', project, path, _options));
                }
                if (page.protocols.mqtt && page.protocols.mqtt.enabled) {
                    rules.push(createTemplateRuleFor('mqtt', project, path, _options));
                }
                if (page.protocols.nfc && page.protocols.nfc.enabled) {
                    rules.push(createTemplateRuleFor('nfc', project, path, _options));
                }
                return schematics_1.chain([
                    angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
                        schematics_1.template(Object.assign({ defaultProtocol,
                            name,
                            page }, project.toTemplate())),
                        schematics_1.move(path)
                    ], {
                        overwrite: _options.overwrite,
                        format: true
                    }),
                    ...rules
                ]);
            }
        },
        schematics_1.schematic('update-routing', {})
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map