import { jsx, jsxs } from "react/jsx-runtime";
import clsx from "clsx";
import { Page } from "@wordpress/admin-ui";
import { __ } from "@wordpress/i18n";
import { DataForm } from "@wordpress/dataviews";
import { useDispatch, useSelect } from "@wordpress/data";
import { store as coreDataStore } from "@wordpress/core-data";
import { __experimentalVStack as VStack } from "@wordpress/components";
import { useState, useMemo, useEffect } from "@wordpress/element";
import { privateApis as editorPrivateApis } from "@wordpress/editor";
import { unlock } from "../../lock-unlock";
import usePatternSettings from "../page-patterns/use-pattern-settings";
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
const { usePostFields, PostCardPanel } = unlock(editorPrivateApis);
const fieldsWithBulkEditSupport = [
  "title",
  "status",
  "date",
  "author",
  "discussion"
];
function PostEditForm({ postType, postId }) {
  const ids = useMemo(() => postId.split(","), [postId]);
  const { record, hasFinishedResolution } = useSelect(
    (select) => {
      const args = ["postType", postType, ids[0]];
      const {
        getEditedEntityRecord,
        hasFinishedResolution: hasFinished
      } = select(coreDataStore);
      return {
        record: ids.length === 1 ? getEditedEntityRecord(...args) : null,
        hasFinishedResolution: hasFinished(
          "getEditedEntityRecord",
          args
        )
      };
    },
    [postType, ids]
  );
  const [multiEdits, setMultiEdits] = useState({});
  const { editEntityRecord } = useDispatch(coreDataStore);
  const _fields = usePostFields({ postType });
  const fields = useMemo(
    () => _fields?.map((field) => {
      if (field.id === "status") {
        return {
          ...field,
          elements: field.elements.filter(
            (element) => element.value !== "trash"
          )
        };
      }
      return field;
    }),
    [_fields]
  );
  const form = useMemo(
    () => ({
      layout: {
        type: "panel"
      },
      fields: [
        {
          id: "featured_media",
          layout: {
            type: "regular"
          }
        },
        {
          id: "status",
          label: __("Status & Visibility"),
          children: ["status", "password"]
        },
        "author",
        "date",
        "slug",
        "parent",
        {
          id: "discussion",
          label: __("Discussion"),
          children: ["comment_status", "ping_status"]
        },
        {
          label: __("Template"),
          id: "template",
          layout: {
            type: "regular",
            labelPosition: "side"
          }
        }
      ].filter(
        (field) => ids.length === 1 || fieldsWithBulkEditSupport.includes(
          typeof field === "string" ? field : field.id
        )
      )
    }),
    [ids]
  );
  const onChange = (edits) => {
    for (const id of ids) {
      if (edits.status && edits.status !== "future" && record?.status === "future" && new Date(record.date) > /* @__PURE__ */ new Date()) {
        edits.date = null;
      }
      if (edits.status && edits.status === "private" && record.password) {
        edits.password = "";
      }
      editEntityRecord("postType", postType, id, edits);
      if (ids.length > 1) {
        setMultiEdits((prev) => ({
          ...prev,
          ...edits
        }));
      }
    }
  };
  useEffect(() => {
    setMultiEdits({});
  }, [ids]);
  const { ExperimentalBlockEditorProvider } = unlock(
    blockEditorPrivateApis
  );
  const settings = usePatternSettings();
  const fieldsWithDependency = useMemo(() => {
    return fields.map((field) => {
      if (field.id === "template") {
        return {
          ...field,
          Edit: (data) => /* @__PURE__ */ jsx(ExperimentalBlockEditorProvider, { settings, children: /* @__PURE__ */ jsx(field.Edit, { ...data }) })
        };
      }
      return field;
    });
  }, [fields, settings]);
  return /* @__PURE__ */ jsxs(VStack, { spacing: 4, children: [
    /* @__PURE__ */ jsx(PostCardPanel, { postType, postId: ids }),
    hasFinishedResolution && /* @__PURE__ */ jsx(
      DataForm,
      {
        data: ids.length === 1 ? record : multiEdits,
        fields: fieldsWithDependency,
        form,
        onChange
      }
    )
  ] });
}
function PostEdit({ postType, postId }) {
  return /* @__PURE__ */ jsxs(
    Page,
    {
      className: clsx("edit-site-post-edit", {
        "is-empty": !postId
      }),
      label: __("Post Edit"),
      children: [
        postId && /* @__PURE__ */ jsx(PostEditForm, { postType, postId }),
        !postId && /* @__PURE__ */ jsx("p", { children: __("Select a page to edit") })
      ]
    }
  );
}
export {
  PostEdit
};
//# sourceMappingURL=index.js.map
