// packages/editor/src/components/sidebar/dataform-post-summary.js
import { __ } from "@wordpress/i18n";
import { useDispatch, useSelect } from "@wordpress/data";
import { store as coreDataStore } from "@wordpress/core-data";
import { DataForm } from "@wordpress/dataviews";
import { __experimentalVStack as VStack } from "@wordpress/components";
import { useMemo } from "@wordpress/element";
import { useViewConfig } from "@wordpress/views";
import PostCardPanel from "../post-card-panel/index.mjs";
import PostPanelSection from "../post-panel-section/index.mjs";
import { store as editorStore } from "../../store/index.mjs";
import PostTrash from "../post-trash/index.mjs";
import usePostFields from "../post-fields/index.mjs";
import { usePostTemplatePanelMode } from "../post-template/hooks.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var EMPTY_FORM = { layout: { type: "panel" }, fields: [] };
function DataFormPostSummary({ onActionPerformed }) {
  const { postType, postId } = useSelect((select) => {
    const { getCurrentPostType, getCurrentPostId } = select(editorStore);
    return {
      postType: getCurrentPostType(),
      postId: getCurrentPostId()
    };
  }, []);
  const { form: formConfig } = useViewConfig({
    kind: "postType",
    name: postType
  });
  const form = formConfig ?? EMPTY_FORM;
  const record = useSelect(
    (select) => {
      if (!postType || !postId) {
        return null;
      }
      return select(coreDataStore).getEditedEntityRecord(
        "postType",
        postType,
        postId
      );
    },
    [postType, postId]
  );
  const templatePanelMode = usePostTemplatePanelMode();
  const availableTemplates = useSelect((select) => {
    if (select(coreDataStore).getCurrentTheme()?.is_block_theme) {
      return null;
    }
    return select(editorStore).getEditorSettings().availableTemplates ?? {};
  }, []);
  const augmentedRecord = useMemo(() => {
    if (!record || !availableTemplates) {
      return record;
    }
    return {
      ...record,
      available_templates: availableTemplates
    };
  }, [record, availableTemplates]);
  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"
          )
        };
      }
      if (field.id === "template") {
        if (!templatePanelMode) {
          return null;
        }
        if (templatePanelMode === "classic" && Object.keys(availableTemplates ?? {}).length === 0) {
          return {
            ...field,
            readOnly: true,
            render: () => __("Default template")
          };
        }
        return field;
      }
      return field;
    }).filter(Boolean),
    [_fields, templatePanelMode, availableTemplates]
  );
  const onChange = (edits) => {
    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, postId, edits);
  };
  return /* @__PURE__ */ jsx(PostPanelSection, { className: "editor-post-summary", children: /* @__PURE__ */ jsxs(VStack, { spacing: 4, children: [
    /* @__PURE__ */ jsx(
      PostCardPanel,
      {
        postType,
        postId,
        onActionPerformed
      }
    ),
    /* @__PURE__ */ jsx(
      DataForm,
      {
        data: augmentedRecord,
        fields,
        form,
        onChange
      }
    ),
    /* @__PURE__ */ jsx(PostTrash, { onActionPerformed })
  ] }) });
}
export {
  DataFormPostSummary as default
};
//# sourceMappingURL=dataform-post-summary.mjs.map
