import { globalObject, isBrowser } from '@skbkontur/global-object';
export var selectNodeContents = function selectNodeContents(node, start, end) {
  if (!node) {
    return;
  }
  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 removeAllSelections() {
  var selection = globalObject.getSelection == null ? void 0 : globalObject.getSelection();
  if (selection) {
    try {
      // Fix IE from issue not working (https://github.com/skbkontur/retail-ui/issues/1205)
      selection.removeAllRanges();
    } catch (e) {

      // empty block
    }
  }
};