/**
 * DevExtreme (cjs/__internal/ui/text_box/m_utils.caret.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = void 0;
var _devices = _interopRequireDefault(require("../../../core/devices"));
var _dom_adapter = _interopRequireDefault(require("../../../core/dom_adapter"));
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _type = require("../../../core/utils/type");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const {
    ios: ios,
    mac: mac
} = _devices.default.real();
const isFocusingOnCaretChange = ios || mac;
const getCaret = function(input) {
    let range;
    try {
        range = {
            start: input.selectionStart,
            end: input.selectionEnd
        }
    } catch (e) {
        range = {
            start: 0,
            end: 0
        }
    }
    return range
};
const setCaret = function(input, position) {
    const body = _dom_adapter.default.getBody();
    if (!body.contains(input) && !body.contains(input.getRootNode().host)) {
        return
    }
    try {
        input.selectionStart = position.start;
        input.selectionEnd = position.end
    } catch (e) {}
};
const caret = function(input, position) {
    let force = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : false;
    input = (0, _renderer.default)(input).get(0);
    if (!(0, _type.isDefined)(position)) {
        return getCaret(input)
    }
    if (!force && isFocusingOnCaretChange && _dom_adapter.default.getActiveElement(input) !== input) {
        return
    }
    setCaret(input, position)
};
var _default = exports.default = caret;
