"use client";
import { useEffect, useRef, useSyncExternalStore } from "react";
import { useMakeswiftHostApiClient } from "../host-api-client";
import { useDocumentLocale } from "./use-document-context";
function useSwatch(swatchId) {
  const client = useMakeswiftHostApiClient();
  const readSwatch = () => swatchId == null ? null : client.readSwatch(swatchId);
  const swatch = useSyncExternalStore(client.subscribe, readSwatch, readSwatch);
  useEffect(() => {
    if (swatchId != null)
      client.fetchSwatch(swatchId).catch(console.error);
  }, [client, swatchId]);
  return swatch;
}
function useSwatches(swatchIds) {
  const client = useMakeswiftHostApiClient();
  const lastSnapshot = useRef(null);
  function getSnapshot() {
    const swatches2 = swatchIds.map((swatchId) => client.readSwatch(swatchId));
    if (lastSnapshot.current != null && lastSnapshot.current.length === swatches2.length && lastSnapshot.current.every((swatch, idx) => swatches2[idx] === swatch)) {
      return lastSnapshot.current;
    }
    return lastSnapshot.current = swatches2;
  }
  const swatches = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
  useEffect(() => {
    Promise.all(swatchIds.map((swatchId) => client.fetchSwatch(swatchId))).catch(console.error);
  }, [client, swatchIds]);
  return swatches;
}
function useFile(fileId) {
  const client = useMakeswiftHostApiClient();
  const readFile = () => fileId == null ? null : client.readFile(fileId);
  const file = useSyncExternalStore(client.subscribe, readFile, readFile);
  useEffect(() => {
    if (fileId != null)
      client.fetchFile(fileId);
  }, [client, fileId]);
  return file;
}
function useFiles(fileIds) {
  const client = useMakeswiftHostApiClient();
  const lastSnapshot = useRef(null);
  function getSnapshot() {
    const files2 = fileIds.map((fileId) => client.readFile(fileId));
    if (lastSnapshot.current != null && lastSnapshot.current.length === files2.length && lastSnapshot.current.every((file, idx) => files2[idx] === file)) {
      return lastSnapshot.current;
    }
    return lastSnapshot.current = files2;
  }
  const files = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
  useEffect(() => {
    Promise.all(fileIds.map((fileId) => client.fetchFile(fileId))).catch(console.error);
  }, [client, fileIds]);
  return files;
}
function useTypography(typographyId) {
  const client = useMakeswiftHostApiClient();
  const readTypography = () => typographyId == null ? null : client.readTypography(typographyId);
  const typography = useSyncExternalStore(client.subscribe, readTypography, readTypography);
  useEffect(() => {
    if (typographyId != null)
      client.fetchTypography(typographyId).catch(console.error);
  }, [client, typographyId]);
  return typography;
}
function useGlobalElement(globalElementId) {
  const client = useMakeswiftHostApiClient();
  const readGlobalElement = () => globalElementId == null ? null : client.readGlobalElement(globalElementId);
  const globalElement = useSyncExternalStore(client.subscribe, readGlobalElement, readGlobalElement);
  useEffect(() => {
    if (globalElementId != null)
      client.fetchGlobalElement(globalElementId).catch(console.error);
  }, [client, globalElementId]);
  return globalElement;
}
function useLocalizedGlobalElement(locale, globalElementId) {
  const client = useMakeswiftHostApiClient();
  const readLocalizedGlobalElement = () => locale == null || globalElementId == null ? null : client.readLocalizedGlobalElement({ globalElementId, locale });
  const localizedGlobalElement = useSyncExternalStore(
    client.subscribe,
    readLocalizedGlobalElement,
    readLocalizedGlobalElement
  );
  useEffect(() => {
    if (locale != null && globalElementId != null) {
      client.fetchLocalizedGlobalElement({ globalElementId, locale }).catch(console.error);
    }
  }, [client, locale, globalElementId]);
  return localizedGlobalElement;
}
function usePagePathnameSlice(pageId) {
  const client = useMakeswiftHostApiClient();
  const locale = useDocumentLocale();
  const readPagePathnameSlice = () => pageId == null ? null : client.readPagePathnameSlice({ pageId, locale });
  const pagePathnameSlice = useSyncExternalStore(
    client.subscribe,
    readPagePathnameSlice,
    readPagePathnameSlice
  );
  useEffect(() => {
    if (pageId != null)
      client.fetchPagePathnameSlice({ pageId, locale }).catch(console.error);
  }, [client, pageId, locale]);
  return pagePathnameSlice;
}
function useTable(tableId) {
  const client = useMakeswiftHostApiClient();
  const readTable = () => tableId == null ? null : client.readTable(tableId);
  const table = useSyncExternalStore(client.subscribe, readTable, readTable);
  useEffect(() => {
    if (tableId != null)
      client.fetchTable(tableId).catch(console.error);
  }, [client, tableId]);
  return table;
}
export {
  useFile,
  useFiles,
  useGlobalElement,
  useLocalizedGlobalElement,
  usePagePathnameSlice,
  useSwatch,
  useSwatches,
  useTable,
  useTypography
};
//# sourceMappingURL=makeswift-api.js.map