"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var post_edit_exports = {};
__export(post_edit_exports, {
  PostEdit: () => PostEdit
});
module.exports = __toCommonJS(post_edit_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_clsx = __toESM(require("clsx"));
var import_admin_ui = require("@wordpress/admin-ui");
var import_i18n = require("@wordpress/i18n");
var import_dataviews = require("@wordpress/dataviews");
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_editor = require("@wordpress/editor");
var import_lock_unlock = require("../../lock-unlock");
var import_use_pattern_settings = __toESM(require("../page-patterns/use-pattern-settings"));
var import_block_editor = require("@wordpress/block-editor");
const { usePostFields, PostCardPanel } = (0, import_lock_unlock.unlock)(import_editor.privateApis);
const fieldsWithBulkEditSupport = [
  "title",
  "status",
  "date",
  "author",
  "discussion"
];
function PostEditForm({ postType, postId }) {
  const ids = (0, import_element.useMemo)(() => postId.split(","), [postId]);
  const { record, hasFinishedResolution } = (0, import_data.useSelect)(
    (select) => {
      const args = ["postType", postType, ids[0]];
      const {
        getEditedEntityRecord,
        hasFinishedResolution: hasFinished
      } = select(import_core_data.store);
      return {
        record: ids.length === 1 ? getEditedEntityRecord(...args) : null,
        hasFinishedResolution: hasFinished(
          "getEditedEntityRecord",
          args
        )
      };
    },
    [postType, ids]
  );
  const [multiEdits, setMultiEdits] = (0, import_element.useState)({});
  const { editEntityRecord } = (0, import_data.useDispatch)(import_core_data.store);
  const _fields = usePostFields({ postType });
  const fields = (0, import_element.useMemo)(
    () => _fields?.map((field) => {
      if (field.id === "status") {
        return {
          ...field,
          elements: field.elements.filter(
            (element) => element.value !== "trash"
          )
        };
      }
      return field;
    }),
    [_fields]
  );
  const form = (0, import_element.useMemo)(
    () => ({
      layout: {
        type: "panel"
      },
      fields: [
        {
          id: "featured_media",
          layout: {
            type: "regular"
          }
        },
        {
          id: "status",
          label: (0, import_i18n.__)("Status & Visibility"),
          children: ["status", "password"]
        },
        "author",
        "date",
        "slug",
        "parent",
        {
          id: "discussion",
          label: (0, import_i18n.__)("Discussion"),
          children: ["comment_status", "ping_status"]
        },
        {
          label: (0, import_i18n.__)("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
        }));
      }
    }
  };
  (0, import_element.useEffect)(() => {
    setMultiEdits({});
  }, [ids]);
  const { ExperimentalBlockEditorProvider } = (0, import_lock_unlock.unlock)(
    import_block_editor.privateApis
  );
  const settings = (0, import_use_pattern_settings.default)();
  const fieldsWithDependency = (0, import_element.useMemo)(() => {
    return fields.map((field) => {
      if (field.id === "template") {
        return {
          ...field,
          Edit: (data) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ExperimentalBlockEditorProvider, { settings, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(field.Edit, { ...data }) })
        };
      }
      return field;
    });
  }, [fields, settings]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalVStack, { spacing: 4, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PostCardPanel, { postType, postId: ids }),
    hasFinishedResolution && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_dataviews.DataForm,
      {
        data: ids.length === 1 ? record : multiEdits,
        fields: fieldsWithDependency,
        form,
        onChange
      }
    )
  ] });
}
function PostEdit({ postType, postId }) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_admin_ui.Page,
    {
      className: (0, import_clsx.default)("edit-site-post-edit", {
        "is-empty": !postId
      }),
      label: (0, import_i18n.__)("Post Edit"),
      children: [
        postId && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PostEditForm, { postType, postId }),
        !postId && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { children: (0, import_i18n.__)("Select a page to edit") })
      ]
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PostEdit
});
//# sourceMappingURL=index.js.map
