"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get checkSelectionBorders () {
        return checkSelectionBorders;
    },
    get createDefaultCustomBorder () {
        return createDefaultCustomBorder;
    },
    get createDefaultHtBorder () {
        return createDefaultHtBorder;
    },
    get createEmptyBorders () {
        return createEmptyBorders;
    },
    get createId () {
        return createId;
    },
    get createSingleEmptyBorder () {
        return createSingleEmptyBorder;
    },
    get denormalizeBorder () {
        return denormalizeBorder;
    },
    get extendDefaultBorder () {
        return extendDefaultBorder;
    },
    get hasLeftRightTypeOptions () {
        return hasLeftRightTypeOptions;
    },
    get hasStartEndTypeOptions () {
        return hasStartEndTypeOptions;
    },
    get markSelected () {
        return markSelected;
    },
    get normalizeBorder () {
        return normalizeBorder;
    },
    get toInlinePropName () {
        return toInlinePropName;
    }
});
const _object = require("../../helpers/object");
const _mixed = require("../../helpers/mixed");
const _array = require("../../helpers/array");
function createId(row, col) {
    return `border_row${row}col${col}`;
}
function createDefaultCustomBorder() {
    return {
        width: 1,
        color: '#000'
    };
}
function createSingleEmptyBorder() {
    return {
        hide: true
    };
}
function createDefaultHtBorder() {
    return {
        width: 1,
        color: '#000',
        cornerVisible: false
    };
}
function normalizeBorder(border) {
    if ((0, _mixed.isDefined)(border.start) || (0, _mixed.isDefined)(border.left)) {
        border.start = border.start ?? border.left;
    }
    if ((0, _mixed.isDefined)(border.end) || (0, _mixed.isDefined)(border.right)) {
        border.end = border.end ?? border.right;
    }
    delete border.left;
    delete border.right;
    return border;
}
function denormalizeBorder(border) {
    if ((0, _mixed.isDefined)(border.start)) {
        border.left = border.start;
    }
    if ((0, _mixed.isDefined)(border.end)) {
        border.right = border.end;
    }
    return border;
}
function createEmptyBorders(row, col) {
    return {
        id: createId(row, col),
        border: createDefaultHtBorder(),
        row,
        col,
        top: createSingleEmptyBorder(),
        bottom: createSingleEmptyBorder(),
        start: createSingleEmptyBorder(),
        end: createSingleEmptyBorder()
    };
}
function extendDefaultBorder(defaultBorder, customBorder) {
    if ((0, _object.hasOwnProperty)(customBorder, 'border') && customBorder.border) {
        defaultBorder.border = customBorder.border;
    }
    if ((0, _object.hasOwnProperty)(customBorder, 'top') && (0, _mixed.isDefined)(customBorder.top)) {
        if (customBorder.top) {
            if (!(0, _object.isObject)(customBorder.top)) {
                customBorder.top = createDefaultCustomBorder();
            }
            defaultBorder.top = customBorder.top;
        } else {
            customBorder.top = createSingleEmptyBorder();
            defaultBorder.top = customBorder.top;
        }
    }
    if ((0, _object.hasOwnProperty)(customBorder, 'bottom') && (0, _mixed.isDefined)(customBorder.bottom)) {
        if (customBorder.bottom) {
            if (!(0, _object.isObject)(customBorder.bottom)) {
                customBorder.bottom = createDefaultCustomBorder();
            }
            defaultBorder.bottom = customBorder.bottom;
        } else {
            customBorder.bottom = createSingleEmptyBorder();
            defaultBorder.bottom = customBorder.bottom;
        }
    }
    if ((0, _object.hasOwnProperty)(customBorder, 'start') && (0, _mixed.isDefined)(customBorder.start)) {
        if (customBorder.start) {
            if (!(0, _object.isObject)(customBorder.start)) {
                customBorder.start = createDefaultCustomBorder();
            }
            defaultBorder.start = customBorder.start;
        } else {
            customBorder.start = createSingleEmptyBorder();
            defaultBorder.start = customBorder.start;
        }
    }
    if ((0, _object.hasOwnProperty)(customBorder, 'end') && (0, _mixed.isDefined)(customBorder.end)) {
        if (customBorder.end) {
            if (!(0, _object.isObject)(customBorder.end)) {
                customBorder.end = createDefaultCustomBorder();
            }
            defaultBorder.end = customBorder.end;
        } else {
            customBorder.end = createSingleEmptyBorder();
            defaultBorder.end = customBorder.end;
        }
    }
    return defaultBorder;
}
function checkSelectionBorders(hot, direction) {
    let atLeastOneHasBorder = false;
    (0, _array.arrayEach)(hot.getSelectedRange(), (range)=>{
        range.forAll((r, c)=>{
            if (r < 0 || c < 0) {
                return;
            }
            const metaBorders = hot.getCellMeta(r, c).borders;
            if (metaBorders) {
                if (direction) {
                    if (!(0, _object.hasOwnProperty)(metaBorders[direction], 'hide') || metaBorders[direction].hide === false) {
                        atLeastOneHasBorder = true;
                        return false; // breaks forAll
                    }
                } else {
                    atLeastOneHasBorder = true;
                    return false; // breaks forAll
                }
            }
        });
    });
    return atLeastOneHasBorder;
}
function markSelected(label) {
    return `<span class="selected">${String.fromCharCode(10003)}</span>${label}`; // workaround for https://github.com/handsontable/handsontable/issues/1946
}
function hasLeftRightTypeOptions(borders) {
    return borders.some((border)=>(0, _mixed.isDefined)(border.left) || (0, _mixed.isDefined)(border.right));
}
function hasStartEndTypeOptions(borders) {
    return borders.some((border)=>(0, _mixed.isDefined)(border.start) || (0, _mixed.isDefined)(border.end));
}
const physicalToInlinePropNames = new Map([
    [
        'left',
        'start'
    ],
    [
        'right',
        'end'
    ]
]);
function toInlinePropName(propName) {
    return physicalToInlinePropNames.get(propName) ?? propName;
}
