var uniflash;
(function (uniflash) {
    var DSCore = (function () {
        function DSCore(coreName, ds) {
            this.coreName = coreName;
            this.ds = ds;
        }
        DSCore.prototype.setProperties = function () {
            var _this = this;
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return core.settings.set(_this.ds.getUfPropertiesValue().getPropertiesValuesMap(_this.ds.getContext().getCurrentCore().name));
            }, this.coreName);
        };
        DSCore.prototype.getProperties = function () {
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return core.settings.get([])
                    .then(function (ret) {
                    return ret.nameValueMap;
                });
            }, this.coreName);
        };
        DSCore.prototype.getTargetInfo = function () {
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return core.getTargetInfo();
            }, this.coreName);
        };
        DSCore.prototype.getResets = function (optionsObj) {
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                var keepAllResets = (optionsObj && optionsObj.keepAllResets) ? optionsObj.keepAllResets : false;
                return core.targetState.getResets()
                    .then(function (ret) {
                    var availableResetOptions = [];
                    _.forOwn(ret.nameValueMap, function (value, key) {
                        if (keepAllResets || value) {
                            availableResetOptions.push(key);
                        }
                    });
                    return availableResetOptions;
                });
            }, this.coreName);
        };
        DSCore.prototype.performReset = function (reset, optionsObj) {
            var _this = this;
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                if (optionsObj && optionsObj.DisconnectBeforeReset) {
                    return core.targetState.disconnect()
                        .then(function () {
                        return _this.ds._q(function (resolve, reject) {
                            setTimeout(function () {
                                resolve(core.targetState.reset(reset));
                            }, 1000);
                        });
                    })
                        .finally(function () {
                        _this.ds.dsImpl.disconnectAllCores();
                    });
                }
                else {
                    return core.targetState.reset(reset);
                }
            }, this.coreName, optionsObj);
        };
        DSCore.prototype.program = function (programs) {
            var _this = this;
            var options = {};
            // set the runAfterOp flag (do not run for serial devices or if user checkbox is off)
            options['runAfterOp'] = (!this.ds.isSerial() && this.ds.getRunTargetAfterProgramLoad());
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                var propsMap = _this.ds.getUfPropertiesValue().getPropertiesValuesMap(_this.ds.getContext().getCurrentCore().name);
                if (!(_this.ds.dsImpl instanceof uniflash.DSLprfImpl)) {
                    propsMap['ResetOnRestart'] = 1;
                }
                var finalRet;
                return core.settings.set(propsMap)
                    .then(function () {
                    return _this.ds.writeFiles(programs);
                })
                    .then(function (dsProgramFiles) {
                    return core.symbols.loadProgramOnly(dsProgramFiles);
                })
                    .then(function (ret) {
                    _this.ds.getConsole().addMessage(_this.ds.getConsole().msgTypes.STATUS, 'Program Load completed successfully.');
                    finalRet = ret;
                    var reset = _this.ds._ufResets.getReset();
                    if (reset.resetAfterLoad && reset.selectedResetOption) {
                        // since this reset is part of a program operation, add [disconnect = false] option
                        // to prevent disconnecting right after the reset is issued
                        return _this.performReset(reset.selectedResetOption, { disconnect: false });
                    }
                })
                    .then(function () {
                    // wait for idle (ie; OnFileLoaded() to complete) before disconnecting from the device
                    return _this.ds.agent.DS.waitForIdle();
                })
                    .then(function () {
                    return finalRet;
                });
            }, this.coreName, options);
        };
        DSCore.prototype.verify = function (programs) {
            var _this = this;
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return _this.ds.writeFiles(programs)
                    .then(function (dsProgramFiles) {
                    return core.symbols.verifyProgram(dsProgramFiles);
                });
            }, this.coreName);
        };
        DSCore.prototype.flashOperation = function (opStr) {
            var _this = this;
            var options = {};
            if (opStr.substring(0, 4) === 'NCO_') {
                // support for Non-connect operations (NCO) by setting connect option to false
                options['connect'] = false;
            }
            // set the runAfterOp flag (do not run for serial devices, if user checkbox is off, or operation is unlock)
            options['runAfterOp'] = (!this.ds.isSerial() && this.ds.getRunTargetAfterProgramLoad() && (opStr.toLowerCase().indexOf('unlock') === -1));
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return core.settings.set(_this.ds.getUfPropertiesValue().getPropertiesValuesMap(_this.ds.getContext().getCurrentCore().name))
                    .then(function () {
                    return core.flash.performOperation(opStr);
                });
            }, this.coreName, options);
        };
        DSCore.prototype.readMemory = function (addr, typeSize, length, format, optionsObj) {
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return core.memory.read(addr, typeSize, length, format);
            }, this.coreName);
        };
        DSCore.prototype.exportMemoryAsCoff = function (ranges, filePath, optionsObj) {
            var _this = this;
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                filePath = filePath ? filePath : 'export_memory.out';
                var fullPath;
                return _this.ds.dsImpl.writeLocalFile(filePath, '')
                    .then(function (obj) {
                    fullPath = obj.path;
                    return core.memory.exportAsCoff(ranges, fullPath);
                }).then(function (result) {
                    var len = 0;
                    _.each(ranges, function (range) {
                        len += range.length;
                    });
                    return { res: result, path: fullPath, length: len };
                });
            }, this.coreName);
        };
        DSCore.prototype.exportMemoryAsBin = function (range, mauSizeInBits, filePath, optionsObj) {
            var _this = this;
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                filePath = filePath ? filePath : 'export_memory.bin';
                var fullPath;
                return _this.ds.dsImpl.writeLocalFile(filePath, '')
                    .then(function (obj) {
                    fullPath = obj.path;
                    return core.memory.exportAsBin(range, mauSizeInBits, fullPath);
                }).then(function (result) {
                    return { res: result, path: fullPath, length: range.length };
                });
            }, this.coreName);
        };
        DSCore.prototype.evaluate = function (expression) {
            return this.ds.dsImpl.completeWorkflowAction(function (core) {
                return core.expressions.evaluate(expression);
            }, this.coreName);
        };
        return DSCore;
    }());
    uniflash.DSCore = DSCore;
})(uniflash || (uniflash = {}));
