import { jsx, jsxs } from "react/jsx-runtime";
import {
  Button,
  TextControl,
  Notice,
  CheckboxControl,
  __experimentalVStack as VStack,
  __experimentalHStack as HStack
} from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import { useSelect, useDispatch } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { decodeEntities } from "@wordpress/html-entities";
import { useState } from "@wordpress/element";
import DialogWrapper from "./dialog-wrapper";
function LinkUIPageCreator({
  postType,
  onBack,
  onPageCreated,
  initialTitle = ""
}) {
  const [title, setTitle] = useState(initialTitle);
  const [shouldPublish, setShouldPublish] = useState(false);
  const isTitleValid = title.trim().length > 0;
  const { lastError, isSaving } = useSelect(
    (select) => ({
      lastError: select(coreStore).getLastEntitySaveError(
        "postType",
        postType
      ),
      isSaving: select(coreStore).isSavingEntityRecord(
        "postType",
        postType
      )
    }),
    [postType]
  );
  const { saveEntityRecord } = useDispatch(coreStore);
  async function createPage(event) {
    event.preventDefault();
    if (isSaving || !isTitleValid) {
      return;
    }
    try {
      const savedRecord = await saveEntityRecord(
        "postType",
        postType,
        {
          title,
          status: shouldPublish ? "publish" : "draft"
        },
        { throwOnError: true }
      );
      if (savedRecord) {
        const pageLink = {
          id: savedRecord.id,
          type: postType,
          title: decodeEntities(savedRecord.title.rendered),
          url: savedRecord.link,
          kind: "post-type"
        };
        onPageCreated(pageLink);
      }
    } catch (error) {
    }
  }
  const isSubmitDisabled = isSaving || !isTitleValid;
  return /* @__PURE__ */ jsx(
    DialogWrapper,
    {
      className: "link-ui-page-creator",
      title: __("Create page"),
      description: __("Create a new page to add to your Navigation."),
      onBack,
      children: /* @__PURE__ */ jsx(VStack, { className: "link-ui-page-creator__inner", spacing: 4, children: /* @__PURE__ */ jsx("form", { onSubmit: createPage, children: /* @__PURE__ */ jsxs(VStack, { spacing: 4, children: [
        /* @__PURE__ */ jsx(
          TextControl,
          {
            __next40pxDefaultSize: true,
            __nextHasNoMarginBottom: true,
            label: __("Title"),
            onChange: setTitle,
            placeholder: __("No title"),
            value: title
          }
        ),
        /* @__PURE__ */ jsx(
          CheckboxControl,
          {
            __nextHasNoMarginBottom: true,
            label: __("Publish immediately"),
            help: __(
              "If unchecked, the page will be created as a draft."
            ),
            checked: shouldPublish,
            onChange: setShouldPublish
          }
        ),
        lastError && /* @__PURE__ */ jsx(Notice, { status: "error", isDismissible: false, children: lastError.message }),
        /* @__PURE__ */ jsxs(HStack, { spacing: 2, justify: "flex-end", children: [
          /* @__PURE__ */ jsx(
            Button,
            {
              __next40pxDefaultSize: true,
              variant: "tertiary",
              onClick: onBack,
              disabled: isSaving,
              accessibleWhenDisabled: true,
              children: __("Cancel")
            }
          ),
          /* @__PURE__ */ jsx(
            Button,
            {
              __next40pxDefaultSize: true,
              variant: "primary",
              type: "submit",
              isBusy: isSaving,
              "aria-disabled": isSubmitDisabled,
              children: __("Create page")
            }
          )
        ] })
      ] }) }) })
    }
  );
}
export {
  LinkUIPageCreator
};
//# sourceMappingURL=page-creator.js.map
