"use strict";
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);

// packages/block-editor/src/components/link-control/use-create-page.js
var use_create_page_exports = {};
__export(use_create_page_exports, {
  default: () => useCreatePage
});
module.exports = __toCommonJS(use_create_page_exports);
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
function useCreatePage(handleCreatePage) {
  const cancelableCreateSuggestion = (0, import_element.useRef)();
  const [isCreatingPage, setIsCreatingPage] = (0, import_element.useState)(false);
  const [errorMessage, setErrorMessage] = (0, import_element.useState)(null);
  const createPage = async function(suggestionTitle) {
    setIsCreatingPage(true);
    setErrorMessage(null);
    try {
      cancelableCreateSuggestion.current = makeCancelable(
        // Using Promise.resolve to allow createSuggestion to return a
        // non-Promise based value.
        Promise.resolve(handleCreatePage(suggestionTitle))
      );
      return await cancelableCreateSuggestion.current.promise;
    } catch (error) {
      if (error && error.isCanceled) {
        return;
      }
      setErrorMessage(
        error.message || (0, import_i18n.__)(
          "An unknown error occurred during creation. Please try again."
        )
      );
      throw error;
    } finally {
      setIsCreatingPage(false);
    }
  };
  (0, import_element.useEffect)(() => {
    return () => {
      if (cancelableCreateSuggestion.current) {
        cancelableCreateSuggestion.current.cancel();
      }
    };
  }, []);
  return {
    createPage,
    isCreatingPage,
    errorMessage
  };
}
var makeCancelable = (promise) => {
  let hasCanceled_ = false;
  const wrappedPromise = new Promise((resolve, reject) => {
    promise.then(
      (val) => hasCanceled_ ? reject({ isCanceled: true }) : resolve(val),
      (error) => hasCanceled_ ? reject({ isCanceled: true }) : reject(error)
    );
  });
  return {
    promise: wrappedPromise,
    cancel() {
      hasCanceled_ = true;
    }
  };
};
//# sourceMappingURL=use-create-page.js.map
