/**
 * @fileoverview added by tsickle
 * Generated from: lib/tap-config-list/tap-config-list.component.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
import { __assign, __awaiter, __generator, __values } from "tslib";
import { Component, Input } from '@angular/core';
import { ToastController } from '@ionic/angular';
import { ConnectionState } from '@iotize/tap/protocol/api';
import { CurrentDeviceService } from '@iotize/ionic';
import { defaultInfoResolverConfig } from '../config';
var TapConfigListComponent = /** @class */ (function () {
    function TapConfigListComponent(tapService, toastController) {
        this.tapService = tapService;
        this.toastController = toastController;
        // dataSource: DeviceInfoDataSource;
        this.header = undefined;
        this.globalRefresh = true;
        this.itemRefresh = false;
        this.lines = 'full';
        this.columns = [
            'key',
            'value'
            // , 'action'
        ];
        this.loading = false;
        this._items = [];
    }
    Object.defineProperty(TapConfigListComponent.prototype, "items", {
        set: /**
         * @param {?} items
         * @return {?}
         */
        function (items) {
            if (!items) {
                items = [];
            }
            items = items.map((/**
             * @param {?} item
             * @return {?}
             */
            function (item) {
                /** @type {?} */
                var defaultOption = defaultInfoResolverConfig.find((/**
                 * @param {?} defaultOption
                 * @return {?}
                 */
                function (defaultOption) { return item.key === defaultOption.key; }));
                if (defaultOption) {
                    item = __assign(__assign({}, defaultOption), item);
                }
                ;
                return item;
            }));
            this._items = items;
        },
        enumerable: true,
        configurable: true
    });
    ;
    /**
     * @return {?}
     */
    TapConfigListComponent.prototype.ngOnInit = /**
     * @return {?}
     */
    function () {
        var _this = this;
        this.sub = this.tapService
            .connectionState
            .subscribe((/**
         * @param {?} event
         * @return {?}
         */
        function (event) {
            var e_1, _a;
            switch (event.newState) {
                case ConnectionState.CONNECTED:
                    try {
                        for (var _b = __values(_this._items), _c = _b.next(); !_c.done; _c = _b.next()) {
                            var item = _c.value;
                            if (item.resolvedValue && item.resolvedValue.error && item.resolvedValue.error) {
                                // console.log(`Item ${item.key} has an error:`, item.resolvedValue.error);
                            }
                        }
                    }
                    catch (e_1_1) { e_1 = { error: e_1_1 }; }
                    finally {
                        try {
                            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                        }
                        finally { if (e_1) throw e_1.error; }
                    }
                    // 
                    break;
            }
        }));
    };
    /**
     * @return {?}
     */
    TapConfigListComponent.prototype.ngOnDestroy = /**
     * @return {?}
     */
    function () {
        this._items = [];
        if (this.sub) {
            this.sub.unsubscribe();
        }
    };
    /**
     * @return {?}
     */
    TapConfigListComponent.prototype.refresh = /**
     * @return {?}
     */
    function () {
        var _this = this;
        this.loading = true;
        this.tapService.infoResolver.refreshKeys(this._items.map((/**
         * @param {?} value
         * @return {?}
         */
        function (value) { return (/** @type {?} */ (value.key)); })))
            .finally((/**
         * @return {?}
         */
        function () { return _this.loading = false; }));
    };
    /**
     * @param {?} event
     * @return {?}
     */
    TapConfigListComponent.prototype.onSubmitValue = /**
     * @param {?} event
     * @return {?}
     */
    function (event) {
    };
    /**
     * @param {?} item
     * @param {?} action
     * @return {?}
     */
    TapConfigListComponent.prototype.clickAction = /**
     * @param {?} item
     * @param {?} action
     * @return {?}
     */
    function (item, action) {
        var _this = this;
        this.loading = true;
        action.call(this.tapService.tap)
            .then((/**
         * @param {?} response
         * @return {?}
         */
        function (response) {
            response.successful();
            if (action.triggersRefresh) {
                return _this.tapService.infoResolver.refreshKeys(action.triggersRefresh);
            }
        }))
            .catch((/**
         * @param {?} err
         * @return {?}
         */
        function (err) {
            _this.onError(err);
        }))
            .then((/**
         * @return {?}
         */
        function () {
            _this.loading = false;
        }));
    };
    /**
     * @param {?} err
     * @return {?}
     */
    TapConfigListComponent.prototype.onError = /**
     * @param {?} err
     * @return {?}
     */
    function (err) {
        return __awaiter(this, void 0, void 0, function () {
            var toast;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        console.warn(err);
                        return [4 /*yield*/, this.toastController.create({
                                message: err.message || 'Unknown error',
                                duration: 3000,
                                position: 'bottom'
                            })];
                    case 1:
                        toast = _a.sent();
                        toast.present();
                        return [2 /*return*/];
                }
            });
        });
    };
    /**
     * @return {?}
     */
    TapConfigListComponent.prototype.loadIfNeeded = /**
     * @return {?}
     */
    function () {
        console.log('TapConfigListComponent', 'load');
        this.tapService.infoResolver.loadKeysIfNotInCache(this._items.map((/**
         * @param {?} value
         * @return {?}
         */
        function (value) { return (/** @type {?} */ (value.key)); })));
    };
    /**
     * @param {?} item
     * @return {?}
     */
    TapConfigListComponent.prototype.isLoaded = /**
     * @param {?} item
     * @return {?}
     */
    function (item) {
        return this.tapService.infoResolver.isLoaded(item.key, this.params);
    };
    TapConfigListComponent.decorators = [
        { type: Component, args: [{
                    selector: 'tap-config-list',
                    template: "<ion-grid *ngIf=\"_items && _items.length > 0\" >\r\n    <ion-row *ngFor=\"let item of _items\" class=\"tap-config-row-{{item.key}}\">\r\n        <ion-col size-xs=\"12\"  size-sm=\"6\">\r\n            <ion-label>\r\n                <span  class=\"item-title\">{{item.title}}</span>\r\n                <ion-icon *ngIf=\"item.icon\" [name]=\"item.icon\"></ion-icon>\r\n                <br *ngIf=\"item.description\"/>\r\n                <span class=\"item-description\">{{item.description}}</span>\r\n            </ion-label>\r\n        </ion-col>\r\n        <ion-col size-xs=\"12\" size-sm=\"6\">\r\n            <tap-config-item *ngIf=\"!item.actions\" [info]=\"item\" [refresh]=\"itemRefresh\" [params]=\"params\"></tap-config-item>\r\n\r\n            <ion-buttons *ngIf=\"item.actions\">\r\n                <ion-button *ngFor=\"let action of item.actions\" [color]=\"action.color\" (click)=\"clickAction(item, action)\" [disabled]=\"loading\">\r\n                    <ion-icon *ngIf=\"action.icon\" [name]=\"action.icon\"></ion-icon>\r\n                    {{action.title}}\r\n                </ion-button>\r\n            </ion-buttons>\r\n        </ion-col>\r\n        <!-- <ion-spinner *ngIf=\"!isLoaded(item)\" slot=\"end\"></ion-spinner> -->\r\n    </ion-row>\r\n    <ion-toolbar *ngIf=\"globalRefresh\">\r\n        <ion-button [disabled]=\"loading\" float-right (click)=\"refresh()\" size=\"small\">\r\n            <ion-icon name=\"refresh\"></ion-icon>\r\n        </ion-button>\r\n    </ion-toolbar>\r\n</ion-grid>\r\n",
                    styles: ["table{width:100%}.item-description{font-size:.8em;color:#777}.item-title{color:#333;font-weight:700;font-size:1.05em}"]
                }] }
    ];
    /** @nocollapse */
    TapConfigListComponent.ctorParameters = function () { return [
        { type: CurrentDeviceService },
        { type: ToastController }
    ]; };
    TapConfigListComponent.propDecorators = {
        header: [{ type: Input }],
        globalRefresh: [{ type: Input }],
        itemRefresh: [{ type: Input }],
        lines: [{ type: Input }],
        columns: [{ type: Input }],
        params: [{ type: Input }],
        items: [{ type: Input }]
    };
    return TapConfigListComponent;
}());
export { TapConfigListComponent };
if (false) {
    /** @type {?} */
    TapConfigListComponent.prototype.header;
    /** @type {?} */
    TapConfigListComponent.prototype.globalRefresh;
    /** @type {?} */
    TapConfigListComponent.prototype.itemRefresh;
    /** @type {?} */
    TapConfigListComponent.prototype.lines;
    /** @type {?} */
    TapConfigListComponent.prototype.columns;
    /** @type {?} */
    TapConfigListComponent.prototype.params;
    /** @type {?} */
    TapConfigListComponent.prototype.loading;
    /**
     * @type {?}
     * @private
     */
    TapConfigListComponent.prototype.sub;
    /** @type {?} */
    TapConfigListComponent.prototype._items;
    /**
     * @type {?}
     * @protected
     */
    TapConfigListComponent.prototype.tapService;
    /**
     * @type {?}
     * @protected
     */
    TapConfigListComponent.prototype.toastController;
    /* Skipping unhandled member: ;*/
}
//# sourceMappingURL=data:application/json;base64, 