/**
 * @since Last modified: 2024/04/20 14:45:15
 * @function offset
 * @description Obtain the absolute offset of the compared to the entire document.
 * @param {getEl} elem  - node element.
 * @returns {object} - Return a object include "left" and "top".
 * @see {@link https://codepen.io/axui/pen/bGzQEPE|demo @ codepen}
 * @example
 * ax.offset('#demo');
 * <!--return {left:0,top:0}-->
 */



'use strict';
import { T_sel } from '../../../types/ax';
import getEl from './getEl';
const offset = (elem: T_sel) => {
    let totalLeft: number,
        totalTop: number,
        scrollLeft: number = document.documentElement.scrollLeft || document.body.scrollLeft,
        scrollTop: number = document.documentElement.scrollTop || document.body.scrollTop,
        targetEl = getEl(elem);
    if (!targetEl) { throw new Error(`The target node does not exist!`) }
    totalLeft = targetEl.getBoundingClientRect().left + scrollLeft;
    totalTop = targetEl.getBoundingClientRect().top + scrollTop;
    return {
        left: totalLeft,
        top: totalTop
    }
}
export default offset;
