var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var uniflash;
(function (uniflash) {
    var DSDualCoreC28Impl = (function (_super) {
        __extends(DSDualCoreC28Impl, _super);
        function DSDualCoreC28Impl() {
            _super.apply(this, arguments);
        }
        DSDualCoreC28Impl.matches = function (partnum) {
            for (var _i = 0, _a = DSDualCoreC28Impl.deviceFilter; _i < _a.length; _i++) {
                var filter = _a[_i];
                if (filter.test(partnum)) {
                    return true;
                }
            }
            //nothing matches
            return false;
        };
        DSDualCoreC28Impl.prototype.completeWorkflowAction = function (mainAction, coreName, optionObj) {
            var _this = this;
            this.$rootScope.$emit('ds.opStarted');
            var curCore;
            return this.configureDS(this.ufSession.getCCXML(), 'uniflash_temp.ccxml')
                .then(function (dsconfigObj) {
                var coresMap = {};
                var matchingCoreName;
                angular.forEach(dsconfigObj.configData.cores, function (core) {
                    if (core.indexOf('CLA') === -1) {
                        coresMap[core] = _this.connectToCore(core, dsconfigObj);
                        if (core.indexOf(coreName) !== -1) {
                            if (!matchingCoreName) {
                                matchingCoreName = core;
                            }
                            else {
                                throw 'core name "' + coreName + '" matches multiple cores on this device.';
                            }
                        }
                    }
                });
                if (!matchingCoreName) {
                    throw 'core name "' + coreName + '" does not match the cores on this device.';
                }
                return _this.$q.all(coresMap)
                    .then(function () {
                    return coresMap[matchingCoreName];
                });
            })
                .then(function (core) {
                if (core && mainAction) {
                    curCore = core;
                    return core.targetState.halt()
                        .catch(function (e) {
                        if (e.message !== 'Target is not running') {
                            _this.$log.error('Halt target failed: ' + e.message);
                            throw e;
                        }
                    });
                }
            })
                .then(function () {
                if (curCore) {
                    return mainAction(curCore);
                }
            })
                .catch(function (error) {
                if (error !== 'backdrop click' &&
                    !(error instanceof ti_common.CancelError)) {
                    return _this.ticAlerts.showError(error.message);
                }
            })
                .finally(function () {
                return _this.$q(function (resolve) {
                    if (curCore) {
                        resolve(curCore.settings.get([])
                            .then(function (ret) {
                            _this.ufPropertiesValue.updateFromDS(ret.nameValueMap, _this.ufContext.getCurrentCore().name);
                        }));
                    }
                    else {
                        resolve();
                    }
                })
                    .then(function () {
                    if (optionObj && (optionObj.connect === false)) {
                        // don't need to run target if we are not connecting to the target (ie; NCOperation)
                        return;
                    }
                    if (curCore && optionObj && optionObj.runAfterOp) {
                        return curCore.targetState.run(1).catch(function (e) {
                            if (e) {
                                _this.$log.error('completeWorkflowAction curCore.targetState.run(1) ' + JSON.stringify(e));
                            }
                        });
                    }
                })
                    .then(function () {
                    if (optionObj && (optionObj.connect === false || optionObj.disconnect === false)) {
                        // don't need to disconnect if we did not connect (ie; NCOperation), or if we don't want to disconnect
                        return;
                    }
                    if (_this.ufPreferencesSrv.get('Auto Disconnect')) {
                        return _this.disconnectAllCores().catch(function (e) {
                            if (e) {
                                _this.$log.error('completeWorkflowAction this.disconnectAllCores() ' + JSON.stringify(e));
                            }
                        });
                    }
                })
                    .finally(function () {
                    _this.$rootScope.$emit('ds.opFinished');
                });
            });
        };
        DSDualCoreC28Impl.deviceFilter = []; // [/.*F2837.*D/i, /.*F2847.*D/i];
        return DSDualCoreC28Impl;
    }(uniflash.DSDefaultImpl));
    uniflash.DSDualCoreC28Impl = DSDualCoreC28Impl;
})(uniflash || (uniflash = {}));
