import type { Flair } from './FlairStore.svelte';
import { get } from './utils';

export type AsEnum<T> = T[keyof T];

export const EventType = Object.freeze({
    Creation: 0,
    StatusUpdate: 1,
    View: 2
});

export type ApplicationEvent = {
    id: string;
    application_id: string;
    event_type: AsEnum<typeof EventType>;
    new_status_id: string;
    time: string;
};

export type Application = {
    id: string;
    url: string;
    simple_url: string;
    title: string;
    user_id: string;
    extra_data: string;
    payrange: string;
    status_id: string;
    recruiter: string;
    agency: boolean;
    company: string;
    message: string;
    linked_application: string;
    create_time: string;
    status_history: string;
    job_level: string;
    location: string;
    inperson_type: string;
    flairs: Flair[];
    events: ApplicationEvent[];
    urls: string[];
};

function createApplicationStore() {
    let all: Application[] = $state([]);

    let dragApplication: Application | undefined = $state(undefined);

    let loadItem: Application | undefined = $state(undefined);
    let loadItemOpen = $state(true);

    let req = false;

    return {
        /**
         * @throws {Error}
         */
        async loadAll(force = false) {
            if (req) return;
            if (!force && all.length > 1) {
                return;
            }
            req = true;
            try {
                all = await get('application/list');
            } finally {
                req = false;
            }
        },

        dragStart(application: Application | undefined) {
            if (!application) {
                return;
            }
            dragApplication = application;
        },

        dragEnd() {
            dragApplication = undefined;
        },

        removeByID(uuid: string) {
            all = all.filter((i) => i.id !== uuid);
        },

        getIndexById(uuid: string): number | undefined {
            return all
                .map((a, i) => [a, i] as [Application, number])
                .filter((i) => i[0].id === uuid)[0]?.[1];
        },

        get dragging() {
            return dragApplication;
        },

        get all() {
            return all;
        },

        reset() {
            // This just tells svelte that we changed all
            all = all;
        },

        set(item: Application, index?: number) {
            if (index === undefined) {
                index = this.getIndexById(item.id);
            }
            if (index === undefined) return;
            all[index] = item;
            this.reset();
        },

        get loadItem() {
            return loadItem;
        },

        set loadItem(item: Application | undefined) {
            loadItem = item;
            loadItemOpen = true;
        },

        get loadItemOpen() {
            return loadItemOpen;
        },

        set loadItemOpen(_loadItemOpen: boolean) {
            loadItemOpen = _loadItemOpen;
        }
    };
}

export const applicationStore = createApplicationStore();
