import {createFeatureSelector, createSelector} from "@ngrx/store";
import {AuthenticationState} from "../authentication-state";

export const authenticationStateFeature = createFeatureSelector<AuthenticationState>('authentication');

export const selectIsLoggedIn = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => state.instanceName !== undefined && state.accessToken !== undefined && state.account !== undefined
)

export const selectDataForAuthorizedRequest = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => {
        return {
            instanceName: state.instanceName,
            accessToken: state.accessToken,
            accountId: state.account?.id,
        }
    }
);
export const selectDataForAuthorizationFlow = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => {
        return {
            instanceName: state.instanceName,
            clientId: state.clientId,
            clientSecret: state.clientSecret,
            redirectUrl: state.redirectUrl,
        }
    }
);

export const selectCurrentInstance = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => state.instanceName
)
export const selectCurrentAccount = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => state.account
)
export const selectCurrentInstanceWithApplicationRegisteredState = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => {
        return {instanceName: state.instanceName, applicationRegistered: state.applicationRegistered};
    }
)

export const selectRegisteringApplicationFlag = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => state.registeringApplication
)
export const selectApplicationRegisteredFlag = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => state.applicationRegistered
)

export const selectAuthorizingUserFlag = createSelector(
    authenticationStateFeature,
    (state: AuthenticationState) => state.authorizingUser
)
