(function (root, factory) {
  if (typeof module === 'object' && module.exports) module.exports = factory;
  else root.createUpdateReadPosition = factory;
})(typeof self !== 'undefined' ? self : this, function createUpdateReadPosition(options) {
  const panel = options.panel;
  const jump = options.jump;
  let identity = '';
  let openingTop = 0;
  let movedByUser = false;
  let positioned = false;
  let target = null;

  function moveToTarget() {
    if (!target) return false;
    target.classList?.add('update-new-activity');
    target.scrollIntoView({ block:'end' });
    positioned = true;
    jump.hidden = true;
    return true;
  }

  panel.addEventListener('scroll', () => {
    if (identity && !positioned && panel.scrollTop !== openingTop) movedByUser = true;
  }, { passive:true });

  return {
    open(nextIdentity) {
      identity = String(nextIdentity || '');
      openingTop = panel.scrollTop;
      movedByUser = false;
      positioned = false;
      target = null;
      jump.hidden = true;
    },
    ready(readyIdentity, newest) {
      if (!identity || String(readyIdentity || '') !== identity || positioned || !newest) return false;
      target = newest;
      target.classList?.add('update-new-activity');
      if (movedByUser) {
        jump.hidden = false;
        return false;
      }
      return moveToTarget();
    },
    jump() {
      return moveToTarget();
    },
  };
});
