var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
// tslint:disable: no-implicit-dependencies
import '@iotize/tap';
import '@iotize/tap/service/all';
import { sleep } from '@iotize/common/utility';
import { StepOperations } from '@iotize/common/task-manager';
import { READ_CONFIGURATOR_CONFIG } from './generated/read-configurator-config';
function stepEvent(observer, step, context, extraData) {
    observer.next(Object.assign({ step, progress: {
            loaded: context.index + 1,
            total: context.total
        } }, (extraData || {})));
}
/**
 * Configure tap from a configuration file schema
 *
 */
export class TapConfiguratorReader {
    constructor(calls = READ_CONFIGURATOR_CONFIG) {
        this.calls = calls;
    }
    createReadOperations(tap) {
        const configureContext = {
            tap
        };
        const operations = [];
        // const calls = READ_CONFIGURATOR_CONFIG;
        const outputConfig = {};
        this.addConfigurCalls(configureContext, this.calls, operations, outputConfig, tap);
        return StepOperations.sequential(operations);
    }
    addConfigurCalls(configureContext, calls, operations, outputConfig, tap) {
        let index = 0;
        for (const key in calls) {
            const context = {
                firmwareVersion: '9999.9999',
                service: tap.service,
                param: undefined // TODO 
            };
            const configKey = key;
            const factoryFct = calls[configKey];
            if (!factoryFct) {
                index++;
                continue;
            }
            const callOrError = factoryFct(context);
            operations.push((observer, context) => __awaiter(this, void 0, void 0, function* () {
                if (callOrError instanceof Error) {
                    stepEvent(observer, TapConfiguratorReader.Step.readKeyError, context, {
                        configKey,
                        error: callOrError,
                    });
                }
                else {
                    try {
                        outputConfig[configKey] = (yield configureContext.tap.lwm2m.execute(callOrError)).body();
                        stepEvent(observer, TapConfiguratorReader.Step.readKeySuccess, context, {
                            configKey,
                            value: outputConfig[configKey],
                            call: callOrError
                        });
                    }
                    catch (error) {
                        stepEvent(observer, TapConfiguratorReader.Step.readKeyError, context, {
                            configKey,
                            call: callOrError,
                            error
                        });
                    }
                }
            }));
            index++;
        }
        operations.push((observer, context) => __awaiter(this, void 0, void 0, function* () {
            stepEvent(observer, TapConfiguratorReader.Step.done, context, {
                config: outputConfig
            });
        }));
    }
    sleep(delayMs) {
        return sleep(delayMs);
    }
}
(function (TapConfiguratorReader) {
    let Step;
    (function (Step) {
        Step["readKeySuccess"] = "TapConfiguratorReaderreadKeySuccess";
        Step["readKeyError"] = "TapConfiguratorReaderReadKeyError";
        Step["done"] = "TapConfiguratorReaderDone";
    })(Step = TapConfiguratorReader.Step || (TapConfiguratorReader.Step = {}));
})(TapConfiguratorReader || (TapConfiguratorReader = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWNvbmZpZ3VyYXRvci1yZWFkZXIuanMiLCJzb3VyY2VSb290Ijoibmc6Ly9AaW90aXplL3RhcC9jb25maWd1cmF0b3IvIiwic291cmNlcyI6WyJsaWIvdGFwLWNvbmZpZ3VyYXRvci1yZWFkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O0FBQUEsMkNBQTJDO0FBQzNDLE9BQU8sYUFBYSxDQUFDO0FBQ3JCLE9BQU8seUJBQXlCLENBQUM7QUFFakMsT0FBTyxFQUFFLEtBQUssRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBRy9DLE9BQU8sRUFBRSxjQUFjLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUs3RCxPQUFPLEVBQWlCLHdCQUF3QixFQUFFLE1BQU0sc0NBQXNDLENBQUM7QUFRL0YsU0FBUyxTQUFTLENBQWdDLFFBQXVCLEVBQUUsSUFBZ0MsRUFBRSxPQUFZLEVBQUUsU0FBYTtJQUNwSSxRQUFRLENBQUMsSUFBSSxpQkFDVC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