"use strict";
"use client";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var makeswift_api_exports = {};
__export(makeswift_api_exports, {
  useFile: () => useFile,
  useFiles: () => useFiles,
  useGlobalElement: () => useGlobalElement,
  useLocalizedGlobalElement: () => useLocalizedGlobalElement,
  usePagePathnameSlice: () => usePagePathnameSlice,
  useSwatch: () => useSwatch,
  useSwatches: () => useSwatches,
  useTable: () => useTable,
  useTypography: () => useTypography
});
module.exports = __toCommonJS(makeswift_api_exports);
var import_react = require("react");
var import_host_api_client = require("../host-api-client");
var import_use_document_context = require("./use-document-context");
function useSwatch(swatchId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const readSwatch = () => swatchId == null ? null : client.readSwatch(swatchId);
  const swatch = (0, import_react.useSyncExternalStore)(client.subscribe, readSwatch, readSwatch);
  (0, import_react.useEffect)(() => {
    if (swatchId != null)
      client.fetchSwatch(swatchId).catch(console.error);
  }, [client, swatchId]);
  return swatch;
}
function useSwatches(swatchIds) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const lastSnapshot = (0, import_react.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 = (0, import_react.useSyncExternalStore)(client.subscribe, getSnapshot, getSnapshot);
  (0, import_react.useEffect)(() => {
    Promise.all(swatchIds.map((swatchId) => client.fetchSwatch(swatchId))).catch(console.error);
  }, [client, swatchIds]);
  return swatches;
}
function useFile(fileId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const readFile = () => fileId == null ? null : client.readFile(fileId);
  const file = (0, import_react.useSyncExternalStore)(client.subscribe, readFile, readFile);
  (0, import_react.useEffect)(() => {
    if (fileId != null)
      client.fetchFile(fileId);
  }, [client, fileId]);
  return file;
}
function useFiles(fileIds) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const lastSnapshot = (0, import_react.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 = (0, import_react.useSyncExternalStore)(client.subscribe, getSnapshot, getSnapshot);
  (0, import_react.useEffect)(() => {
    Promise.all(fileIds.map((fileId) => client.fetchFile(fileId))).catch(console.error);
  }, [client, fileIds]);
  return files;
}
function useTypography(typographyId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const readTypography = () => typographyId == null ? null : client.readTypography(typographyId);
  const typography = (0, import_react.useSyncExternalStore)(client.subscribe, readTypography, readTypography);
  (0, import_react.useEffect)(() => {
    if (typographyId != null)
      client.fetchTypography(typographyId).catch(console.error);
  }, [client, typographyId]);
  return typography;
}
function useGlobalElement(globalElementId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const readGlobalElement = () => globalElementId == null ? null : client.readGlobalElement(globalElementId);
  const globalElement = (0, import_react.useSyncExternalStore)(client.subscribe, readGlobalElement, readGlobalElement);
  (0, import_react.useEffect)(() => {
    if (globalElementId != null)
      client.fetchGlobalElement(globalElementId).catch(console.error);
  }, [client, globalElementId]);
  return globalElement;
}
function useLocalizedGlobalElement(locale, globalElementId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const readLocalizedGlobalElement = () => locale == null || globalElementId == null ? null : client.readLocalizedGlobalElement({ globalElementId, locale });
  const localizedGlobalElement = (0, import_react.useSyncExternalStore)(
    client.subscribe,
    readLocalizedGlobalElement,
    readLocalizedGlobalElement
  );
  (0, import_react.useEffect)(() => {
    if (locale != null && globalElementId != null) {
      client.fetchLocalizedGlobalElement({ globalElementId, locale }).catch(console.error);
    }
  }, [client, locale, globalElementId]);
  return localizedGlobalElement;
}
function usePagePathnameSlice(pageId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const locale = (0, import_use_document_context.useDocumentLocale)();
  const readPagePathnameSlice = () => pageId == null ? null : client.readPagePathnameSlice({ pageId, locale });
  const pagePathnameSlice = (0, import_react.useSyncExternalStore)(
    client.subscribe,
    readPagePathnameSlice,
    readPagePathnameSlice
  );
  (0, import_react.useEffect)(() => {
    if (pageId != null)
      client.fetchPagePathnameSlice({ pageId, locale }).catch(console.error);
  }, [client, pageId, locale]);
  return pagePathnameSlice;
}
function useTable(tableId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const readTable = () => tableId == null ? null : client.readTable(tableId);
  const table = (0, import_react.useSyncExternalStore)(client.subscribe, readTable, readTable);
  (0, import_react.useEffect)(() => {
    if (tableId != null)
      client.fetchTable(tableId).catch(console.error);
  }, [client, tableId]);
  return table;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFile,
  useFiles,
  useGlobalElement,
  useLocalizedGlobalElement,
  usePagePathnameSlice,
  useSwatch,
  useSwatches,
  useTable,
  useTypography
});
//# sourceMappingURL=makeswift-api.js.map