import {Grove, PossibleNodes} from "./Grove";
import {TestableCompositeNode} from "./TestableCompositeNode";
import { TestableNode } from "./TestableNode";
import {StateBranchNode} from "./StateBranchNode";
import {NodesStore, useNodesStore} from "./store";

export const getNodesFinder = () => {
    const state = useNodesStore.getState();

    return NodesFinder.fromState(state);
}
/**
 * This class is designed to be used OUTSIDE a Node class (eg: NOT inside a TestableNode or BranchNode, etc.)
 *
 * For example
 *      -Inside the ConnectingLines React component
 *      -Inside any other react component or hook
 */
export class NodesFinder {
    constructor(
        private readonly grove: Grove
    ) {}

    /**
     * Does not support raw testable data (eg NOT a TestableCompositeNode or a TestableNode, but the raw data from the testables array.)
     * If you need to get the testable data, get its testable node here and then from there you can get the raw testable data.
     */
    get<Expected = PossibleNodes | undefined>(id: string) {
        if (id.startsWith('group') || id.startsWith('branch')) {
            return this.grove.get<Expected>(id);
        }
    }

    static fromState(state: Pick<NodesStore, 'grove'>) {
        return new NodesFinder(Grove.fromState(state));
    }
}