"use client";
import { isNotFound } from "@stigmer/sdk";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
/**
 * Data hook that fetches a single Skill by organization, slug, and
 * optional version.
 *
 * Wraps `stigmer.skill.getByReference()` with loading, error, and
 * not-found state management. When `org`, `slug`, or `version` change,
 * the previous in-flight request is discarded and a fresh fetch begins.
 *
 * Pass `null` for either `org` or `slug` to skip fetching (stable
 * no-op). The `version` parameter is optional — omit it to fetch the
 * latest version. Pass a tag (e.g. `"stable"`) or a content hash to
 * pin to a specific version.
 *
 * **Not-found handling:** If the API returns a 404 (NOT_FOUND), the
 * hook sets `skill` to `null` without raising an error. Consumers
 * distinguish "not found" from "loading" by checking all three fields:
 * `skill === null && !isLoading && !error` means the resource does
 * not exist.
 *
 * @example
 * ```tsx
 * function SkillDetail({ org, slug }: { org: string; slug: string }) {
 *   const { skill, isLoading, error } = useSkill(org, slug);
 *
 *   if (isLoading) return <Skeleton />;
 *   if (error) return <ErrorMessage error={error} />;
 *   if (!skill) return <NotFound />;
 *
 *   return <h1>{skill.metadata?.name}</h1>;
 * }
 * ```
 *
 * @example
 * ```tsx
 * // Fetch a specific tagged version
 * const { skill } = useSkill("acme", "code-style-guide", "stable");
 * ```
 */
export function useSkill(org, slug, version) {
    const stigmer = useStigmer();
    const { data: skill, isLoading, isRefetching, error, refetch } = useFetch(org && slug
        ? async () => {
            try {
                return await stigmer.skill.getByReference({ org, slug, version });
            }
            catch (err) {
                if (isNotFound(err))
                    return null;
                throw err;
            }
        }
        : null, [org, slug, version, stigmer], null);
    return { skill, isLoading, isRefetching, error, refetch };
}
//# sourceMappingURL=useSkill.js.map