/**
 * Applies attributes to user popouts.
 * @module User
 * @memberof Builtin.Attributes.Components.Popouts
 */

import { patch, unpatch } from '@vizality/patcher';
import { getModule } from '@vizality/webpack';

export const labels = [ 'Components', 'Popouts', 'User' ];
  
export default builtin => {
  const UserPopout = getModule(m => m?.type?.displayName === 'UserPopoutContainer');
  patch('vz-attributes-popout-user', UserPopout, 'type', (_, res) => {
    try {
      res.props.setPopoutRef = elem => {
        if (elem) {
          elem.setAttribute('vz-user-id', res?.props?.user?.id);
          elem.parentElement.setAttribute('vz-popout', 'user');
        }
      };
    } catch (err) {
      return builtin.error(builtin._labels.concat(labels), err);
    }
  });
  return () => unpatch('vz-attributes-popout-user');
};
