"use client";
import { useCallback, useMemo, useState } from "react";
let nextId = 0;
function uid() {
    return `ws-${++nextId}-${Date.now()}`;
}
function deriveNameFromGitUrl(url) {
    const cleaned = url.replace(/\/+$/, "").replace(/\.git$/, "");
    const segments = cleaned.split("/");
    if (segments.length >= 2) {
        return `${segments[segments.length - 2]}/${segments[segments.length - 1]}`;
    }
    return segments[segments.length - 1] ?? url;
}
function deriveNameFromPath(path) {
    return path.replace(/[/\\]+$/, "") || path;
}
/**
 * Behavior hook that manages a workspace entry array with add, remove,
 * validate, and name-derivation logic.
 *
 * Encapsulates the non-trivial logic that platform builders should
 * not reimplement: URL validation, name derivation from git URLs or
 * local paths, and conversion to the SDK input shape.
 *
 * @example
 * ```tsx
 * function SessionSetup() {
 *   const workspace = useWorkspaceEntries();
 *
 *   return (
 *     <div>
 *       <WorkspaceEditor workspace={workspace} />
 *       <SessionComposer
 *         onSubmit={(msg) => createSession(msg, workspace.toInput())}
 *         workspace={workspace}
 *       />
 *     </div>
 *   );
 * }
 * ```
 *
 * @example
 * ```tsx
 * // Programmatic entry management
 * const workspace = useWorkspaceEntries();
 * workspace.addGitRepo("https://github.com/acme/api.git", "main");
 * workspace.addLocalPath("/Users/dev/projects/frontend");
 * ```
 */
export function useWorkspaceEntries() {
    const [entries, setEntries] = useState([]);
    const addGitRepo = useCallback((url, branch) => {
        const name = deriveNameFromGitUrl(url);
        setEntries((prev) => [
            ...prev,
            { id: uid(), name, type: "git", gitUrl: url, gitBranch: branch },
        ]);
    }, []);
    const addLocalPath = useCallback((path) => {
        const name = deriveNameFromPath(path);
        setEntries((prev) => [
            ...prev,
            { id: uid(), name, type: "local", localPath: path },
        ]);
    }, []);
    const remove = useCallback((id) => {
        setEntries((prev) => prev.filter((e) => e.id !== id));
    }, []);
    const clear = useCallback(() => {
        setEntries([]);
    }, []);
    const clearLocal = useCallback(() => {
        setEntries((prev) => prev.filter((e) => e.type !== "local"));
    }, []);
    const toInput = useCallback(() => {
        return entries.map((entry) => {
            const source = entry.type === "git"
                ? { gitRepo: { url: entry.gitUrl, branch: entry.gitBranch } }
                : { localPath: { path: entry.localPath } };
            return { name: entry.name, source };
        });
    }, [entries]);
    const hasEntries = entries.length > 0;
    return useMemo(() => ({
        entries,
        addGitRepo,
        addLocalPath,
        remove,
        clear,
        clearLocal,
        toInput,
        hasEntries,
    }), [entries, addGitRepo, addLocalPath, remove, clear, clearLocal, toInput, hasEntries]);
}
//# sourceMappingURL=useWorkspaceEntries.js.map