"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 strings_1 = require("@angular-devkit/core/src/utils/strings");
const schematics_1 = require("@angular-devkit/schematics");
const iotize_studio_1 = require("@iotize/device-client.js/config/iotize-studio");
const lib_1 = require("../../lib");
const angular_utility_1 = require("../../lib/angular-utility");
const templateHelpers = __importStar(require("../../lib/template-helpers"));
const type_converter_1 = require("./type-converter");
const utility_1 = require("@iotize/common/utility");
// import { camelize } from '@angular-devkit/core/src/utils/strings';
function variableToType(variable) {
    if ((variable.length || 1) > 1) {
        return `number[] // ${variable.type} with ${variable.length} items`;
    }
    else {
        return `number // ${variable.type}`;
    }
}
exports.variableToType = variableToType;
// You don't have to export the function as default. You can also have more than one rule factory
// per file.
// This is the factory function 
function main(_options) {
    return (tree, _context) => {
        var _a;
        const project = lib_1.loadProject(tree, _context);
        const tapConfig = project.tapConfig;
        const sourceTemplates = schematics_1.url('./files');
        const { name, path } = lib_1.getAngularProjectPath(_options.name || 'tap-api', tree, _options);
        if (!tapConfig.config.data) {
            tapConfig.config.data = {};
        }
        const bundles = tapConfig.config.data.bundles || [];
        const iotizeStudioConfig = project.iotizeStudioConfig;
        if (iotizeStudioConfig) {
            bundles.forEach(bundle => {
                (bundle.variables || []).forEach(variable => {
                    if (!iotizeStudioConfig) {
                        throw utility_1.assertError(`iotizeStudioConfig should not be null`);
                    }
                    const iotizeStudioVariableConfig = iotizeStudioConfig.variable(variable.id.toString());
                    const byteOrder = iotizeStudioVariableConfig.byteOrder();
                    if (byteOrder) {
                        variable._options = {
                            byteOrder: byteOrder in iotize_studio_1.ByteOrder32Bits ? iotize_studio_1.ByteOrder32Bits[byteOrder] : iotize_studio_1.ByteOrder16Bits[byteOrder]
                        };
                    }
                });
            });
        }
        if (bundles.length === 0) {
            return schematics_1.noop();
        }
        const bundlesConfig = JSON.stringify(bundles, null, 4);
        // TODO we need to compute converters ? 
        return angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
            schematics_1.template(Object.assign(Object.assign(Object.assign(Object.assign({ name, appName: ((_a = tapConfig.config.application) === null || _a === void 0 ? void 0 : _a.name) || 'TAP' }, tapConfig), { variableToType: variableToType, bundleName: (input) => {
                    return strings_1.camelize(input);
                }, variableName: (input) => {
                    return lib_1.variablenamify(input);
                }, bundlesConfig,
                bundles, createConverterInstance: (variable) => {
                    return type_converter_1.TypeConverterHelper.deduceConverter(variable);
                } }), templateHelpers), project.toTemplate())),
            schematics_1.move(path)
        ], {
            overwrite: _options.overwrite
        });
    };
}
exports.main = main;
//# sourceMappingURL=index.js.map