import { useDebounceCallback } from "../../../../../node_modules/.pnpm/usehooks-ts@3.1.1_react@19.1.1/node_modules/usehooks-ts/dist/index.js";
import { useEditorHelpers } from "../../../../../use-editor.js";
import { useDraft, usePage, useSite, useSectionsSubscribe, usePageAttributesSubscribe, useSiteAttributesSubscribe, useThemeSubscribe } from "../../../../../use-page-data.js";
const AUTO_SAVE_MIN_INTERVAL = 1e3;
function usePageAutoSave() {
  const draft = useDraft();
  const pageConfig = usePage();
  const site = useSite();
  const { onSavePage, onSaveSite } = useEditorHelpers();
  const savePage = useDebounceCallback(async (data) => {
    await onSavePage?.({
      pageId: pageConfig.id,
      pageVersionId: "latest",
      siteId: site.id,
      data
    });
    draft.setDirty(false);
  }, AUTO_SAVE_MIN_INTERVAL);
  const saveSite = useDebounceCallback(async (data) => {
    await onSaveSite?.({
      siteId: site.id,
      data
    });
    draft.setDirty(false);
  }, AUTO_SAVE_MIN_INTERVAL);
  useSectionsSubscribe((sections) => {
    console.debug("Sections have changed, updating page version", sections);
    savePage({ sections });
  });
  usePageAttributesSubscribe((attributes) => {
    console.debug("Page attributes have changed:", attributes);
    savePage({ attributes });
  });
  useSiteAttributesSubscribe((attributes) => {
    console.debug("Site attributes have changed:", attributes);
    saveSite({ attributes });
  });
  useThemeSubscribe((theme) => {
    console.debug("theme has changed, updating page version");
    saveSite({ theme });
  });
}
export {
  usePageAutoSave
};
//# sourceMappingURL=use-page-autosave.js.map
