import { TextObjectFilterResult } from "@models/TextObjectFilter.model";
import { create } from "zustand";

export type TextObjectModalContent = {
    header?: React.ReactNode;
    bottom?: React.ReactNode;
    renderResult: (result: TextObjectFilterResult, index: number) => React.ReactNode;
};

interface ITextObjectModalStore {
    content: TextObjectModalContent;
    data: TextObjectFilterResult[];
    endpoint: string;
    onSelected: (item: TextObjectFilterResult) => void;
    setContent: (content: TextObjectModalContent) => void;
    setData: (data: TextObjectFilterResult[]) => void;
    setEndPoint: (endpoint: string) => void;
    setOnSelected: (onSelected: (item: TextObjectFilterResult) => void) => void;
    clear: () => void;
}

export type TextObjectModalState = ITextObjectModalStore;

const useTextObjectModalStore = create<TextObjectModalState>()((set) => ({
    content: {
        header: undefined,
        bottom: undefined,
        renderResult: () => undefined,
    },
    data: [],
    endpoint: "",
    onSelected: () => {},
    setContent: (content: TextObjectModalContent) => set(() => ({ content: content })),
    setData: (data: TextObjectFilterResult[]) => set(() => ({ data: data })),
    setEndPoint: (endpoint: string) => set(() => ({ endpoint: endpoint })),
    setOnSelected(onSelected: (item: TextObjectFilterResult) => void) {
        set(() => ({ onSelected: onSelected }));
    },
    clear: () =>
        set(() => ({
            content: {
                header: undefined,
                bottom: undefined,
                renderResult: () => undefined,
            },
            data: [],
            endpoint: "",
        })),
}));

export { useTextObjectModalStore };
