"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMarkPersistent = useMarkPersistent;
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _store = require("../../store");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function useMarkPersistent({
  html,
  value
}) {
  const previousText = (0, _element.useRef)();
  const hasActiveFormats = !!value.activeFormats?.length;
  const {
    __unstableMarkLastChangeAsPersistent
  } = (0, _data.useDispatch)(_store.store);

  // Must be set synchronously to make sure it applies to the last change.
  (0, _element.useLayoutEffect)(() => {
    // Ignore mount.
    if (!previousText.current) {
      previousText.current = value.text;
      return;
    }

    // Text input, so don't create an undo level for every character.
    // Create an undo level after 1 second of no input.
    if (previousText.current !== value.text) {
      const timeout = window.setTimeout(() => {
        __unstableMarkLastChangeAsPersistent();
      }, 1000);
      previousText.current = value.text;
      return () => {
        window.clearTimeout(timeout);
      };
    }
    __unstableMarkLastChangeAsPersistent();
  }, [html, hasActiveFormats]);
}
//# sourceMappingURL=use-mark-persistent.js.map