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());
    });
};
import { StepOperations } from '@iotize/common/task-manager';
import { sleep } from '@iotize/common/utility';
import '@iotize/tap';
import '@iotize/tap/service/all';
import { READ_CONFIGURATOR_CONFIG, } from './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 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,
                config: outputConfig,
            };
            const configKey = key;
            const factoryFct = calls[configKey];
            if (!factoryFct) {
                index++;
                continue;
            }
            const callOrErrorOrUndefined = factoryFct(context);
            if (callOrErrorOrUndefined) {
                operations.push((observer, context) => __awaiter(this, void 0, void 0, function* () {
                    if (callOrErrorOrUndefined instanceof Error) {
                        stepEvent(observer, TapConfiguratorReader.Step.readKeyError, context, {
                            configKey,
                            error: callOrErrorOrUndefined,
                            // call: callOrError
                        });
                    }
                    else {
                        try {
                            outputConfig[configKey] = (yield configureContext.tap.lwm2m.execute(callOrErrorOrUndefined)).body();
                            stepEvent(observer, TapConfiguratorReader.Step.readKeySuccess, context, {
                                configKey,
                                value: outputConfig[configKey],
                                call: callOrErrorOrUndefined,
                            });
                        }
                        catch (error) {
                            stepEvent(observer, TapConfiguratorReader.Step.readKeyError, context, {
                                configKey,
                                call: callOrErrorOrUndefined,
                                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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWNvbmZpZ3VyYXRvci1yZWFkZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9jb25maWd1cmF0b3Ivc3JjL2xpYi90YXAtY29uZmlndXJhdG9yLXJlYWRlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFFQSxPQUFPLEVBQUUsY0FBYyxFQUFFLE1BQU0sNkJBQTZCLENBQUM7QUFDN0QsT0FBTyxFQUFFLEtBQUssRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBQy9DLE9BQU8sYUFBYSxDQUFDO0FBRXJCLE9BQU8seUJBQXlCLENBQUM7QUFHakMsT0FBTyxFQUVMLHdCQUF3QixHQUN6QixNQUFNLDRCQUE0QixDQUFDO0FBTXBDLFNBQVMsU0FBUyxDQUNoQixRQUF1QixFQUN2QixJQUFnQyxFQUNoQyxPQUFZLEVBQ1osU0FBYTtJQU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