export interface IOffsetBounding {
  top: number,
  bottom: number,
  left: number,
  right: number,
  height: number,
  width: number
}

export class DomUtils {
  static getOffsetBounding(element: HTMLElement): IOffsetBounding {
    let bounding = {
      top: 0,
      bottom: 0,
      left: 0,
      right: 0,
      height: element.offsetHeight,
      width: element.offsetWidth,
    } as IOffsetBounding;
    while (element) {
      bounding.top += element.offsetTop;
      bounding.left += element.offsetLeft;
      if (!element.offsetParent) {
        if (element.parentElement && ['BODY', 'HTML'].indexOf(element.parentElement.tagName) > -1) {
          element = element.parentElement;
          continue;
        }
        bounding.right = element.offsetWidth - bounding.left - bounding.width;
        bounding.bottom = element.offsetHeight - bounding.top - bounding.height;
        break;
      }
      element = element.offsetParent as HTMLElement;
    }

    return bounding;
  }
}
