import { SharedState, useWatchSharedState } from "@vertesia/ui/core";
import { createContext, useContext } from "react";
export class ObjectTypeSearch {
    client;
    limit;
    result = new SharedState({ objects: [], isLoading: false });
    query = {};
    constructor(client, limit = 100) {
        this.client = client;
        this.limit = limit;
    }
    get objects() {
        return this.result.value.objects;
    }
    get error() {
        return this.result.value.error;
    }
    get isRunning() {
        return this.result.value.isLoading;
    }
    getFilterValue(name) {
        return this.query[name];
    }
    setFilterValue(name, value) {
        this.query[name] = value;
        this.search();
    }
    reset(isLoading = false) {
        this.result.value = {
            objects: [],
            isLoading
        };
    }
    _updateRunningState(value) {
        const prev = this.result.value;
        this.result.value = {
            objects: prev.objects,
            isLoading: value,
            error: prev.error
        };
    }
    async _search(loadMore = false) {
        if (this.isRunning) {
            return Promise.resolve(false);
        }
        this.result.value = {
            isLoading: true,
            objects: loadMore ? this.objects : [],
        };
        const limit = this.limit;
        const offset = this.objects.length;
        return this.client.types.list({
            limit,
            offset,
            query: this.query
        }).then(async (result) => {
            this.result.value = {
                isLoading: false,
                objects: this.objects.concat(result)
            };
            return true;
        }).catch((err) => {
            this.result.value = {
                error: err,
                isLoading: false,
                objects: this.objects
            };
            throw err;
        });
    }
    search() {
        if (this.isRunning) {
            return Promise.resolve(false);
        }
        return this._search(false);
    }
    loadMore() {
        if (this.isRunning) {
            return Promise.resolve(false);
        }
        return this._search(true);
    }
}
const ObjectTypeSearchContext = createContext(undefined);
export function useSearch() {
    return useContext(ObjectTypeSearchContext);
}
export function useWatchSearchResult() {
    const search = useSearch();
    const result = useWatchSharedState(search.result);
    return { ...result, search };
}
export function useSearchCount() {
    const search = useSearch();
    return search.objects.length;
}
export { ObjectTypeSearchContext };
//# sourceMappingURL=ObjectTypeSearchContext.js.map