"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var underscore_1 = __importDefault(require("underscore"));
var Utils = /** @class */ (function () {
    function Utils() {
        return;
    }
    Object.defineProperty(Utils, "DEFAULT_CELL_STYLE", {
        get: function () {
            return {
                'background-color': 'FFFFFF',
                color: '000000',
                'font-family': 'Arial',
                'font-size': '11pt',
                'font-style': 'normal',
                'font-weight': 'normal',
                height: '20px',
                'number-format': '',
                'text-align': 'left',
                'text-wrap': 'normal',
                'vertical-align': 'middle',
                width: '64px',
                tbs: 'none',
                rbs: 'none',
                bbs: 'none',
                lbs: 'none',
                tbc: '000000',
                rbc: '000000',
                bbc: '000000',
                lbc: '000000',
                tbw: 'none',
                rbw: 'none',
                bbw: 'none',
                lbw: 'none',
            };
        },
        enumerable: true,
        configurable: true
    });
    Utils.prototype.parseApiError = function (err, def) {
        if (def === void 0) { def = ''; }
        var msg = def;
        if (err.data) {
            var keys = Object.keys(err.data);
            if (keys.length) {
                if (underscore_1.default.isArray(err.data[keys[0]])) {
                    msg = err.data[keys[0]][0];
                }
                else if (typeof err.data[keys[0]] === 'string') {
                    msg = err.data[keys[0]];
                }
                else {
                    msg = def;
                }
            }
            else {
                msg = def;
            }
        }
        else {
            msg = def;
        }
        return msg;
    };
    Utils.prototype.clonePageContent = function (content, removeStyles) {
        var copy = [];
        var deltaStyle = {};
        for (var i = 0; i < content.length; i++) {
            copy[i] = [];
            for (var j = 0; j < content[i].length; j++) {
                var cell = __assign({}, content[i][j]);
                var style = {};
                for (var k in cell.style) {
                    if (cell.style[k] === deltaStyle[k])
                        continue;
                    style[k] = cell.style[k];
                }
                deltaStyle = __assign({}, cell.style);
                cell.style = removeStyles ? {} : style;
                if (cell.index)
                    delete cell.index;
                // if (cell.formatting) delete cell.formatting;
                copy[i].push(cell);
            }
        }
        return copy;
    };
    Utils.prototype.comparePageContent = function (currentContent, newContent, ignore_styles) {
        if (ignore_styles === void 0) { ignore_styles = false; }
        if (!currentContent || !newContent) {
            return false;
        }
        var diffContent = [];
        var sizeDiff = false;
        // compare
        for (var i = 0; i < newContent.length; i++) {
            for (var k = 0; k < newContent[i].length; k++) {
                var diff = false;
                if (currentContent[i] && currentContent[i][k]) {
                    // check for value changes
                    if (newContent[i][k].value !== currentContent[i][k].value) {
                        diff = true;
                    }
                    // check for style changes
                    if (!diff && !ignore_styles) {
                        for (var attr in newContent[i][k].style) {
                            if (newContent[i][k].style[attr] !== currentContent[i][k].style[attr]) {
                                diff = true;
                                if (['width', 'height'].indexOf(attr) > -1)
                                    sizeDiff = true;
                                break;
                            }
                        }
                    }
                }
                else {
                    diff = true;
                }
                if (diff) {
                    if (!diffContent[i]) {
                        diffContent[i] = [];
                    }
                    diffContent[i][k] = newContent[i][k];
                }
            }
        }
        if (diffContent.length ||
            newContent.length < currentContent.length ||
            newContent[0].length < currentContent[0].length) {
            var rowDiff = currentContent.length - newContent.length;
            var colDiff = currentContent[0].length - newContent[0].length;
            return { content: newContent, content_diff: diffContent, colDiff: colDiff, rowDiff: rowDiff, sizeDiff: sizeDiff };
        }
        return false;
    };
    Utils.prototype.mergePageContent = function (_original, _deltas, style) {
        if (style === void 0) { style = {}; }
        var current = [];
        // let style: IPageCellStyle = merge({}, _defaultStyles);
        var colWidths = [];
        for (var rowIndex = 0; rowIndex < _original.length; rowIndex++) {
            var row = _original[rowIndex];
            current[rowIndex] = [];
            for (var colIndex = 0; colIndex < row.length; colIndex++) {
                var col = void 0;
                if (_deltas &&
                    _deltas[rowIndex] &&
                    _deltas[rowIndex][colIndex]) {
                    col = _deltas[rowIndex][colIndex];
                    // col.style = merge({}, style, col.style);
                    col.style = __assign({}, row[colIndex].style, col.style);
                    style = __assign({}, style, row[colIndex].style);
                }
                else {
                    col = __assign({}, row[colIndex]);
                    col.style = __assign({}, style, col.style);
                    delete col.dirty;
                    if (col.checked !== undefined)
                        delete col.checked;
                    style = __assign({}, col.style);
                }
                if (!rowIndex && col.style) {
                    colWidths[colIndex] = col.style.width;
                }
                if (col.style)
                    col.style.width = colWidths[colIndex];
                // col.formatting = {};
                col.index = {
                    row: rowIndex,
                    col: colIndex,
                };
                current[rowIndex].push(col);
            }
        }
        return current;
    };
    Utils.prototype.getDefaultCellStyle = function () {
        return {
            'background-color': 'FFFFFF',
            color: '000000',
            'font-family': 'Arial',
            'font-size': '11pt',
            'font-style': 'normal',
            'font-weight': 'normal',
            height: '20px',
            'number-format': '',
            'text-align': 'left',
            'text-wrap': 'normal',
            'vertical-align': 'middle',
            width: '64px',
            tbs: 'none',
            rbs: 'none',
            bbs: 'none',
            lbs: 'none',
            tbc: '000000',
            rbc: '000000',
            bbc: '000000',
            lbc: '000000',
            tbw: 'none',
            rbw: 'none',
            bbw: 'none',
            lbw: 'none',
        };
    };
    return Utils;
}());
exports.default = Utils;
//# sourceMappingURL=Utils.js.map