import { UserCredential } from "@firebase/auth";
import { UserReadPrivateResponse } from "@newlife/newlife-creator-client-api";
import { derived } from "overmind";
import { CreatorApi, UserInfo, Link } from "../types";
import history from "../history";

type PHONE_VERIFICATION_STATUS = "" | "REQUESTED" | "RECEIVED" | "SUBMITTED" | "FAILED";

const flows = {
    onboarding: {
        status: "",
        domainName: "",
        onboardingForm: <UserReadPrivateResponse>{}
    }
}

export type State = {
    // userInfo: UserInfo | null,
    api: CreatorApi | null,
    auth: {
        status: {
            authenticating: boolean,
            phoneVerification: PHONE_VERIFICATION_STATUS,
            authenticated: boolean
            authorizing: boolean
            authorized: boolean
        },
        user: UserReadPrivateResponse,
        fbUser: UserCredential | {}
    }
    routing: {
        preLoginRoute: string,
        breadcrumbs: Link[],
        history: typeof history,
        location: string
    },
    flows: typeof flows
    indicators: {
        _inProgressCounter: number,
        inProgress: boolean
    }
}

export const newAuth = () => Object.assign({}, {
        status: {
            authenticating: false,
            phoneVerification: "" as PHONE_VERIFICATION_STATUS,
            authenticated: false,
            authorized: false,
            authorizing: false
        },
        user: {},
        fbUser: {}
    });

export const state: State = {
    api: null,
    // userInfo: null,
    auth: newAuth(),
    routing: {
        preLoginRoute: "",
        breadcrumbs: [],
        history,
        location: ""
    },
    indicators: {
        _inProgressCounter: 0,
        inProgress: derived<{ _inProgressCounter: number }, {}, boolean>((state) => state._inProgressCounter > 0)
    },
    flows
}

  