/* eslint-disable require-await */
/* eslint-disable import/prefer-default-export */

import { BLOCKLET_SERVICE_PATH_PREFIX, DID_SPACES_BASE_URL } from '../../constant';

/**
 * @description
 * @export
 * @param {*} user
 * @return {Promise<boolean>}
 */
export async function didSpacesIsRequired(user) {
  if (user?.didSpace?.endpoint) {
    return false;
  }

  if (!window.blocklet) {
    return false;
  }

  // 如果是在 iframe 中，直接返回 false
  if (window.self !== window.top) {
    return false;
  }

  if (window.location.origin === new URL(DID_SPACES_BASE_URL).origin) {
    return false;
  }

  // @note: 如果在 blocklet service 页面中，那么默认不展示 did-spaces 引导
  if (window.location.href.includes(BLOCKLET_SERVICE_PATH_PREFIX)) {
    return false;
  }

  const { componentMountPoints } = window.blocklet;
  const [, componentDid] = window.blocklet.componentId.split('/');
  return componentMountPoints.some((c) => c?.capabilities?.didSpace === 'requiredOnConnect' && componentDid === c.did);
}
