"use client";
import { useEffect, useState } from "react";
import { apiCall } from "../../backend/utils/apiCall.js";
function useTenantContext(orgSlug) {
  const [state, setState] = useState({
    tenantId: void 0,
    organizationId: void 0,
    organizationName: void 0,
    loading: true,
    error: null
  });
  useEffect(() => {
    let cancelled = false;
    async function lookup() {
      if (!orgSlug) {
        setState((prev) => ({ ...prev, loading: false, error: "No organization slug provided." }));
        return;
      }
      try {
        const { ok, result: data } = await apiCall(`/api/directory/organizations/lookup?slug=${encodeURIComponent(orgSlug)}`);
        if (cancelled) return;
        if (!ok || !data?.ok || !data.organization) {
          setState((prev) => ({
            ...prev,
            loading: false,
            error: data?.error || "Organization not found."
          }));
          return;
        }
        setState({
          tenantId: data.organization.tenantId ?? void 0,
          organizationId: data.organization.id,
          organizationName: data.organization.name,
          loading: false,
          error: null
        });
      } catch {
        if (!cancelled) {
          setState((prev) => ({ ...prev, loading: false, error: "Failed to load organization." }));
        }
      }
    }
    lookup();
    return () => {
      cancelled = true;
    };
  }, [orgSlug]);
  return state;
}
export {
  useTenantContext
};
//# sourceMappingURL=useTenantContext.js.map
