import { useMemo } from "react";
import * as ReactPage from "../../../state/react-page";
import { useMakeswiftHostApiClient } from "../host-api-client";
import { useStore } from "./use-store";
import { useDocumentKey, useDocumentLocale } from "./use-document-context";
function useResourceResolver() {
  const store = useStore();
  const client = useMakeswiftHostApiClient();
  const documentKey = useDocumentKey();
  const locale = useDocumentLocale();
  return useMemo(() => {
    return {
      resolveSwatch: (swatchId) => client.resolveSwatch(swatchId),
      resolveFile: (fileId) => client.resolveFile(fileId),
      resolveTypography: (typographyId) => client.resolveTypography(typographyId),
      resolvePagePathnameSlice: (pageId) => client.resolvePagePathnameSlice({ pageId, locale }),
      resolveElementId: (elementKey) => {
        const read = () => documentKey == null ? null : ReactPage.getElementId(store.getState(), documentKey, elementKey);
        let lastValue = null;
        return {
          name: `element-id:${documentKey}:${elementKey}`,
          readStable: () => lastValue = read(),
          subscribe: (onUpdate) => store.subscribe(() => {
            if (read() !== lastValue)
              onUpdate();
          })
        };
      }
    };
  }, [client, store, documentKey, locale]);
}
export {
  useResourceResolver
};
//# sourceMappingURL=use-resource-resolver.js.map