"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 core_1 = require("@angular-devkit/core");
// import { camelize } from '@angular-devkit/core/src/utils/strings';
const lib_1 = require("../../lib");
const templateHelpers = __importStar(require("../../lib/template-helpers"));
const angular_utility_1 = require("../../lib/angular-utility");
const strings_1 = require("@angular-devkit/core/src/utils/strings");
const definitions_1 = require("@iotize/device-config.js/definitions");
const type_converter_1 = require("./type-converter");
function variableToType(variable) {
    if (variable.length > 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) => {
        let project = lib_1.loadProject(tree, _context);
        let tapConfig = project.tapConfig;
        const sourceTemplates = schematics_1.url('./files');
        const { name, path } = lib_1.getAngularProjectPath(_options.name || 'tap-api', tree, _options);
        let bundles = tapConfig.config.data.bundles || [];
        let iotizeStudioConfig = project.iotizeStudioConfig;
        if (iotizeStudioConfig) {
            bundles.forEach(bundle => {
                (bundle.variables || []).forEach(variable => {
                    let iotizeStudioVariableConfig = iotizeStudioConfig.variable(variable.id.toString());
                    let byteOrder = iotizeStudioVariableConfig.byteOrder();
                    if (byteOrder) {
                        variable._options = {
                            byteOrder: byteOrder in definitions_1.ByteOrder32Bits ? definitions_1.ByteOrder32Bits[byteOrder] : definitions_1.ByteOrder16Bits[byteOrder]
                        };
                    }
                });
            });
        }
        // console.info('TEST LOGGER', bundles[0].variables);
        let bundlesConfig = JSON.stringify(bundles, null, 4);
        // TODO we need to compute converters ? 
        return angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
            schematics_1.template(Object.assign({ name, appName: tapConfig.config.application.name }, 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, core_1.strings, project.toTemplate())),
            schematics_1.move(path)
        ], _options.overwrite);
    };
}
exports.main = main;
//# sourceMappingURL=index.js.map