"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 lib_1 = require("../../lib");
const definitions_1 = require("../../lib/definitions");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const angular_utility_1 = require("../../lib/angular-utility");
const IONIC_CORDOVA_PLUGINS = [
    {
        package: '@ionic-native/dialogs',
        version: '^5.0.0',
        id: 'cordova-plugin-dialogs',
        spec: '^2.0.2'
    }
];
const IONIC_DEPENDENCIES_VERSION = "^5.0.0";
const IONIC_DEPENDENCIES = [
    {
        name: '@ionic-native/nfc',
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: '^5.8.0'
    },
    {
        name: '@ionic-native/core',
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: IONIC_DEPENDENCIES_VERSION
    },
    {
        name: '@ionic-native/splash-screen',
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: IONIC_DEPENDENCIES_VERSION
    },
    {
        name: '@ionic-native/status-bar',
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: IONIC_DEPENDENCIES_VERSION
    }
];
const ANGULAR_DEPENDENCIES = [
    {
        name: "@angular/cdk",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^7.2.0"
    },
    {
        name: "@angular/animations",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^7.2.0"
    }
    // {
    //   name: '@agm/core',
    //   overwrite: false,
    //   type: NodeDependencyType.Default,
    //   version: "1.0.0-beta.6"
    // }
];
const IOTIZE_DEPS = [
    {
        name: "@iotize/ionic",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^0.0.1-alpha.37"
    },
    {
        // TODO only required if we authorize app to be run inside an iframe
        name: '@iotize/device-com-webview.js',
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^0.0.1-alpha.5"
    },
    {
        name: "@iotize/device-admin.js",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "0.0.1-alpha.7"
    },
    {
        name: '@iotize/testing-grammar.js',
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "0.0.1-alpha.17"
    },
    // {
    //   name: "@iotize-private/shared-sdk",
    //   overwrite: false,
    //   type: NodeDependencyType.Default,
    //   version: "^2.0.0"
    // },
    {
        name: "@iotize/device-config.js",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^0.0.1-alpha.5"
    },
    {
        name: "@iotize/device-client.js",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^0.0.1-alpha.103"
    },
    {
        name: "@angular/material",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^7.2.0"
    },
    {
        name: "fast-xml-parser",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^3.12.16"
    },
    {
        name: "@swimlane/ngx-charts",
        overwrite: false,
        type: dependencies_1.NodeDependencyType.Default,
        version: "^12.0.1"
    }
];
const IOTIZE_CORDOVA_DEPS = [
    {
        package: '@iotize/device-com-nfc.cordova',
        spec: '1.0.0-alpha.11',
        id: 'cordova-plugin-iotize-device-com-nfc',
        prefixVersionWithPackage: true
    }
];
function addProtocol(project, p) {
    switch (p) {
        case definitions_1.AppProtocol.BLE:
            project.dependencies.addCordovaPlugin([
                {
                    package: '@iotize/cordova-plugin-iotize-ble',
                    id: 'cordova-plugin-iotize-ble',
                    spec: '1.1.0-alpha.30',
                    prefixVersionWithPackage: true
                }
            ]);
            project.dependencies.addNpmDependencies([
                {
                    name: '@types/web-bluetooth',
                    version: '0.0.4',
                    type: dependencies_1.NodeDependencyType.Default
                },
                {
                    name: '@iotize/device-com-web-bluetooth.js',
                    version: '0.0.1-alpha.7',
                    type: dependencies_1.NodeDependencyType.Default
                }
            ]);
            break;
        case definitions_1.AppProtocol.NFC:
            break;
        case definitions_1.AppProtocol.WIFI:
            project.dependencies.addCordovaPlugin([{
                    package: '@iotize/device-com-wifi.cordova',
                    spec: '1.0.0-alpha.1',
                    id: 'cordova-plugin-iotize-device-com-wifi',
                    prefixVersionWithPackage: true
                }]);
            break;
        case definitions_1.AppProtocol.SOCKET:
            project.dependencies.addCordovaPlugin([{
                    package: '@iotize/device-com-socket.cordova',
                    spec: '1.0.0-alpha.1',
                    id: 'cordova-plugin-iotize-device-com-socket',
                    prefixVersionWithPackage: true
                }]);
            break;
        case definitions_1.AppProtocol.WEBSOCKET:
            project.dependencies.addNpmDependencies([
                {
                    name: '@iotize/device-com-websocket.js',
                    version: '0.0.1-alpha.26',
                    overwrite: true,
                    type: dependencies_1.NodeDependencyType.Default
                }
            ]);
            break;
        // case Protocol.WEBSOCKET:
        //   addPackageDependecies(tree, [
        //     '@iotize/device-com-websocket.js'
        //   ]);
        //   break;
        case definitions_1.AppProtocol.MQTT:
            project.dependencies.addNpmDependencies([
                '@iotize-private/device-com-mqtt.js'
            ]);
    }
}
// function addIonicNativeDependecies(tree: Tree) {
//   // addPackageJsonDependency(tree, {
//   //   name: '@ionic-native/nfc',
//   //   overwrite: false,
//   //   type: NodeDependencyType.Default,
//   //   version: '^5.8.0'
//   // });
//   let version = IONIC_DEPENDENCIES_VERSION;
//   for (let depName of IONIC_DEPENDENCIES) {
//     addPackageJsonDependency(
//       tree,
//     );
//   }
//   // for (let dep of IONIC_CORDOVA_PLUGINS) {
//   //   addCordovaPlugin(tree, dep);
//   // }
// }
// function addAngularDependencies(tree: Tree: boolean) {
//   ANGULAR_DEPENDENCIES
//     .forEach(dep => addPackageJsonDependency(tree, dep));
// }
// function addIotizeDependencies(tree: Tree): boolean {
//   IOTIZE_DEPS.forEach((dep) => addPackageJsonDependency(tree, dep));
//   IOTIZE_CORDOVA_DEPS.forEach((dep) => addCordovaPlugin(tree, dep));
// }
function default_1(_options) {
    return schematics_1.chain([
        angular_utility_1.toPromiseRule((tree, _context) => __awaiter(this, void 0, void 0, function* () {
            let project = lib_1.loadProject(tree, _context);
            project
                .dependencies
                .addNpmDependencies([
                ...ANGULAR_DEPENDENCIES,
                ...IOTIZE_DEPS,
                ...IONIC_DEPENDENCIES
            ])
                .addCordovaPlugin([
                ...IOTIZE_CORDOVA_DEPS,
                ...IONIC_CORDOVA_PLUGINS
            ]);
            for (let protocol of project.communicationProtocols) {
                addProtocol(project, protocol);
            }
            if (project.dependencies.hasNewDependencies()) {
                project.logger.info(`New dependencies to install: ${project.dependencies.newDependencies.map(p => p.name).join(',')}`);
                if (!_options.skipInstall) {
                    yield lib_1.shell.execSpawn('npm', [
                        'install'
                    ]).catch(err => {
                        project.logger.error(`Cannot install dependencies: ${err.message}`);
                    });
                }
            }
        }))
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map