"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPosition = getPosition;
exports.getRelativePosition = getRelativePosition;
exports.getTextRelativePosition = getTextRelativePosition;
exports.getSVGRelativePosition = getSVGRelativePosition;
exports.getHTMLRelativePosition = getHTMLRelativePosition;
exports.getParentPosition = getParentPosition;
exports.getScrollPosition = getScrollPosition;
exports.getLocalPosition = getLocalPosition;
const parseTransform_1 = require("../../parseTransform");
const geometry_1 = require("../geometry");
function getPosition(element) {
    if (element instanceof Text) {
        return { x: 0, y: 0 };
    }
    else {
        const { x, y } = element.getBoundingClientRect();
        return { x, y };
    }
}
function getRelativePosition(element, relative) {
    if (element instanceof Text) {
        return getTextRelativePosition(element, relative);
    }
    if (element instanceof SVGElement) {
        return getSVGRelativePosition(element, relative);
    }
    if (element instanceof HTMLElement) {
        return getHTMLRelativePosition(element, relative);
    }
}
// RETURN
function getTextRelativePosition(element, relative) {
    const range = document.createRange();
    range.selectNodeContents(element);
    const rects = range.getClientRects();
    const rect = rects[0] || { x: 0, y: 0 };
    return rect;
}
// RETURN
function getSVGRelativePosition(element, relative) {
    // return NULL_VECTOR
    return element.parentElement
        ? getHTMLRelativePosition(element.parentElement, relative)
        : geometry_1.NULL_VECTOR;
}
function getHTMLRelativePosition(element, relative) {
    const local_position = getLocalPosition(element);
    // const scroll_position = getScrollPosition(element, relative)
    const scroll_position = { x: 0, y: 0 };
    const parent_position = getParentPosition(element, relative);
    return (0, geometry_1.addVector3)(local_position, scroll_position, parent_position);
}
function getParentPosition(element, relative) {
    const { offsetParent } = element;
    let x = 0;
    let y = 0;
    const pushParent = (p) => {
        const local_position = getLocalPosition(p);
        x += local_position.x;
        y += local_position.y;
    };
    let p = offsetParent;
    while (p && p !== relative) {
        pushParent(p);
        p = p.offsetParent;
    }
    return { x, y };
}
function getScrollPosition(element, relative) {
    const { parentElement } = element;
    let x = 0;
    let y = 0;
    const pushScrollParent = (p) => {
        const { scrollLeft, scrollTop } = p;
        x += scrollLeft;
        y += scrollTop;
    };
    let p = parentElement;
    while (p && p !== relative) {
        pushScrollParent(p);
        p = p.parentElement;
    }
    return { x, y };
}
function getLocalPosition(element) {
    const { offsetLeft = 0, offsetTop = 0, offsetWidth = 0, offsetHeight = 0, style, } = element;
    let offset_x = offsetLeft;
    let offset_y = offsetTop;
    let width;
    let height;
    let transform_x;
    let transform_y;
    let scale_x;
    let scale_y;
    const { transform } = style;
    if (width !== offsetWidth || height !== offsetHeight) {
        if (transform) {
            const [_transform_x, _transform_y, _scale_x, _scale_y] = (0, parseTransform_1.parseTransform)(transform, offsetWidth, offsetHeight);
            transform_x = _transform_x;
            transform_y = _transform_y;
            scale_x = _scale_x;
            scale_y = _scale_y;
        }
        else {
            transform_x = 0;
            transform_y = 0;
            scale_x = 1;
            scale_y = 1;
        }
    }
    width = offsetWidth;
    height = offsetHeight;
    // TODO there's more to it
    const x = offsetLeft + transform_x;
    const y = offsetTop + transform_y;
    return { x, y };
}
//# sourceMappingURL=getPosition.js.map