import { ADB2C_SIGN_IN_TYPE } from '../../../constants';
import { AdB2cUser } from '../types/types';

export const isPromiseRejectedResult = (
  data: unknown,
): data is PromiseRejectedResult => {
  return (
    data !== null &&
    typeof data === 'object' &&
    'status' in data &&
    'reason' in data
  );
};

// 生のAdB2cUserのレスポンスから表示名とメールアドレスを取得する
export const getUserNameAndMailAddress = (user: AdB2cUser) => {
  const { displayName, identities } = user;
  const emailAddress = identities?.find(
    (identity) => identity.signInType === ADB2C_SIGN_IN_TYPE.EMAILADDRESS,
  )?.issuerAssignedId;
  return { displayName, emailAddress };
};
