import { useState } from "react";
import type { FeatureCollection } from "@/uhm/types/geo";
import { useBackgroundSessionState } from "@/uhm/lib/editor/session/useBackgroundSessionState";
import { useEntitySessionState } from "@/uhm/lib/editor/session/useEntitySessionState";
import { useProjectSessionState } from "@/uhm/lib/editor/session/useProjectSessionState";
import { useTimelineState } from "@/uhm/lib/editor/session/useTimelineState";
import { useWikiSessionState } from "@/uhm/lib/editor/session/useWikiSessionState";
import type { EditorMode, TimelineRange } from "@/uhm/lib/editor/session/sessionTypes";

export type {
    EditorMode,
    EntityFormState,
    GeometryMetaFormState,
    TimelineRange,
} from "@/uhm/lib/editor/session/sessionTypes";

type Options = {
    emptyFeatureCollection: FeatureCollection;
    defaultEditorUserId: string;
    fallbackTimelineRange: TimelineRange;
    currentYear: number;
};

export function useEditorSessionState(options: Options) {
    // Mode thao tác map/editor hiện tại.
    const [mode, setMode] = useState<EditorMode>("idle");
    // Baseline FeatureCollection used to seed/reset the editor draft for the current session.
    const [baselineFeatureCollection, setBaselineFeatureCollection] = useState<FeatureCollection>(options.emptyFeatureCollection);

    const project = useProjectSessionState({
        defaultEditorUserId: options.defaultEditorUserId,
    });
    const entity = useEntitySessionState();
    const timeline = useTimelineState({
        currentYear: options.currentYear,
        fallbackTimelineRange: options.fallbackTimelineRange,
    });
    const background = useBackgroundSessionState();
    const wiki = useWikiSessionState();

    return {
        mode,
        setMode,
        baselineFeatureCollection,
        setBaselineFeatureCollection,
        ...project,
        ...entity,
        ...timeline,
        ...background,
        ...wiki,
    };
}
