export default function FixedContentOnScroll(element, { parent, siblingContent, discountTop }) {
  const PARENT = parent || element.parentNode,
    SIBLING_CONTENT = siblingContent || null,
    DISCOUNT_TOP = discountTop || 16;

  element.style.position = 'relative';

  window.addEventListener('scroll', function() {
    if (DISCOUNT_TOP != undefined) {
      var discountTop = DISCOUNT_TOP.map(function(item) {
        return item ? item.offsetHeight : 0;
      });

      discountTop = discountTop.reduce(function(accum, curr) {
        return accum + curr;
      });
      discountTop = discountTop + 16;
    }

    if (SIBLING_CONTENT && ELEMENT_INFO.height >= SIBLING_CONTENT.offsetHeight) return;

    const ELEMENT_INFO = element.getBoundingClientRect(),
      PARENT_INFO = PARENT.getBoundingClientRect(),
      STYLE = window.getComputedStyle(PARENT),
      PARENT_PADDING_BOTTOM = parseInt(STYLE.getPropertyValue('padding-bottom').replace('px', '')),
      PARENT_PADDING_TOP = parseInt(STYLE.getPropertyValue('padding-top').replace('px', '')),
      PARENT_PADDING = PARENT_PADDING_BOTTOM + PARENT_PADDING_TOP;

    ELEMENT_INFO.offsetTop = element.offsetTop;
    PARENT_INFO.offsetTop = PARENT.offsetTop;

    const TOP =
      element.style.top && element.style.top != 'initial' ? parseInt(element.style.top.replace('px', ''), 10) : 0;
    if (TOP + ELEMENT_INFO.height > PARENT_INFO.height) {
      element.style.top = 'initial';
    }

    if (ELEMENT_INFO.height == PARENT_INFO.height - PARENT_PADDING) return;

    if (ELEMENT_INFO.height > window.innerHeight) {
      // Quando o conteudo fixo é maior que o viewport
      if (PARENT_INFO.offsetTop + PARENT_PADDING_TOP + ELEMENT_INFO.height <= window.pageYOffset + window.innerHeight) {
        if (
          PARENT_INFO.offsetTop + PARENT_INFO.height - PARENT_PADDING_BOTTOM <=
          window.pageYOffset + window.innerHeight
        ) {
          element.style.top = PARENT_INFO.height - PARENT_PADDING - ELEMENT_INFO.height + 'px';
        } else if (
          ELEMENT_INFO.height + PARENT_INFO.offsetTop + PARENT_PADDING_TOP <=
          window.pageYOffset + window.innerHeight
        ) {
          element.style.top =
            window.pageYOffset -
            PARENT_INFO.offsetTop -
            PARENT_PADDING_TOP -
            (ELEMENT_INFO.height - window.innerHeight) +
            'px';
        }
      } else {
        element.style.top = 'initial';
      }
    } else {
      if (window.pageYOffset + discountTop >= PARENT_INFO.offsetTop + PARENT_PADDING_TOP) {
        if (
          ELEMENT_INFO.height <=
          PARENT_INFO.offsetTop + PARENT_INFO.height - PARENT_PADDING_BOTTOM - window.pageYOffset - discountTop
        ) {
          element.style.top = window.pageYOffset - PARENT_INFO.offsetTop - PARENT_PADDING_TOP + discountTop + 'px';
        } else {
          element.style.top = PARENT_INFO.height - PARENT_PADDING - ELEMENT_INFO.height + 'px';
        }
      } else {
        element.style.top = 'initial';
      }
    }
  });

  window.dispatchEvent(new Event('scroll'));

  var time;
  window.addEventListener('resize', function() {
    clearTimeout(time);

    time = setTimeout(function() {
      window.dispatchEvent(new Event('scroll'));
    }, 60);
  });

  return this;
}
