import { UserRoleType } from '@/modules/@common/user/types';

import { slice } from '../slices';

const nameSpace = slice.name;

export const getUserDataSelector = (state: RootState) => state[nameSpace].userData ?? {};

export const getUserPermissionsSelector = (state: RootState) => state[nameSpace].userData?.permissions ?? [];

export const getUserRoleSelector = (state: RootState) => state[nameSpace].userData?.role ?? ({} as UserRoleType);

export const getUserIdSelector = (state: RootState) => state[nameSpace].userData?.id ?? null;
export const getUserTimezoneSelector = (state: RootState) => state[nameSpace].userData?.timezone;
export const getUserFirstNameSelector = (state: RootState) => state[nameSpace].userData?.firstName ?? 'User';
export const getUserLastNameSelector = (state: RootState) => state[nameSpace].userData?.lastName ?? '';
export const getUserFullNameSelector = (state: RootState) =>
  `${getUserFirstNameSelector(state)} ${getUserLastNameSelector(state)}`;
export const getUserEmailSelector = (state: RootState) => state[nameSpace].userData?.email ?? null;

export const getUserUpdatedTime = (state: RootState) => state[nameSpace].userData?.updateDate ?? null;

export const getUserContentOwnersSelector = (state: RootState) => state[nameSpace].userData?.contentOwners ?? [];
export const getUserContentOwnerIdsSelector = (state: RootState) =>
  state[nameSpace].userData?.contentOwners?.map(({ contentOwnerId }) => contentOwnerId) ?? [];

export const getUserAccountSelector = (state: RootState) => state[nameSpace].userData?.account ?? null;
export const getUserAccountIdSelector = (state: RootState) => state[nameSpace].userData?.accountId ?? null;

export const getPublisherIdsSelector = (state: RootState) => state[nameSpace].userData?.publisherIds ?? null;

export const getPredefinedContentOwnerSelector = (state: RootState) =>
  state[nameSpace].userData?.contentOwnerId ?? null;

export const getOwnsContentSelector = (state: RootState) =>
  getUserContentOwnersSelector(state)
    .filter((owner) => owner.publisherOwnsContent)
    .map((owner) => owner.contentOwnerId);

export const getUserRoleTypeSelector = (state: RootState) => getUserRoleSelector(state)?.type ?? null;

export const getIntercomUserHashSelector = (state: RootState) => state[nameSpace].userData?.intercomUserHash ?? null;

export const userDataForWatch = (state: RootState) => state[nameSpace].userData;
