import { isBrowser, getOwnerGlobalObject } from '../../lib/globalObject.js';
export var selectNodeContents = function (node, start, end) {
    if (!node) {
        return;
    }
    var globalObject = getOwnerGlobalObject(node);
    if (isBrowser(globalObject) && 'createRange' in globalObject.document) {
        try {
            var selection = globalObject.getSelection();
            var range = globalObject.document.createRange();
            if (start !== undefined && end !== undefined) {
                range.setStart(node, start);
                range.setEnd(node, end);
            }
            else {
                range.selectNodeContents(node);
            }
            if (selection !== null) {
                // Fix IE from issue not working (https://github.com/skbkontur/retail-ui/issues/1205)
                selection.removeAllRanges();
                selection.addRange(range);
                return;
            }
        }
        catch (e) {
            // empty block
        }
    }
    if (isBrowser(globalObject) && 'createTextRange' in globalObject.document.body) {
        // @ts-expect-error: IE-specific API.
        var range = globalObject.document.body.createTextRange();
        range.moveToElementText(node);
        if (typeof range.select === 'function') {
            range.select();
        }
        return;
    }
};
export var removeAllSelections = function (globalObject) {
    var _a;
    var selection = (_a = globalObject.getSelection) === null || _a === void 0 ? void 0 : _a.call(globalObject);
    if (selection) {
        try {
            // Fix IE from issue not working (https://github.com/skbkontur/retail-ui/issues/1205)
            selection.removeAllRanges();
        }
        catch (e) {
            // empty block
        }
    }
};
//# sourceMappingURL=selectionHelpers.js.map