"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);
var post_meta_exports = {};
__export(post_meta_exports, {
  default: () => post_meta_default
});
module.exports = __toCommonJS(post_meta_exports);
var import_core_data = require("@wordpress/core-data");
var import_store = require("../store");
var import_lock_unlock = require("../lock-unlock");
function getPostMetaFields(select, context) {
  const { getEditedEntityRecord } = select(import_core_data.store);
  const { getRegisteredPostMeta } = (0, import_lock_unlock.unlock)(select(import_core_data.store));
  let entityMetaValues;
  if (context?.postType && context?.postId) {
    entityMetaValues = getEditedEntityRecord(
      "postType",
      context?.postType,
      context?.postId
    ).meta;
  }
  const registeredFields = getRegisteredPostMeta(context?.postType);
  const metaFields = {};
  Object.entries(registeredFields || {}).forEach(([key, props]) => {
    if (key !== "footnotes" && key.charAt(0) !== "_") {
      metaFields[key] = {
        label: props.title || key,
        value: (
          // When using the entity value, an empty string IS a valid value.
          entityMetaValues?.[key] ?? // When using the default, an empty string IS NOT a valid value.
          (props.default || void 0)
        ),
        type: props.type
      };
    }
  });
  if (!Object.keys(metaFields || {}).length) {
    return null;
  }
  return metaFields;
}
var post_meta_default = {
  name: "core/post-meta",
  getValues({ select, context, bindings }) {
    const metaFields = getPostMetaFields(select, context);
    const newValues = {};
    for (const [attributeName, source] of Object.entries(bindings)) {
      const fieldKey = source.args.key;
      const { value: fieldValue, label: fieldLabel } = metaFields?.[fieldKey] || {};
      newValues[attributeName] = fieldValue ?? fieldLabel ?? fieldKey;
    }
    return newValues;
  },
  setValues({ dispatch, context, bindings }) {
    const newMeta = {};
    Object.values(bindings).forEach(({ args, newValue }) => {
      newMeta[args.key] = newValue;
    });
    dispatch(import_core_data.store).editEntityRecord(
      "postType",
      context?.postType,
      context?.postId,
      {
        meta: newMeta
      }
    );
  },
  canUserEditValue({ select, context, args }) {
    if (context?.query || context?.queryId) {
      return false;
    }
    if (!context?.postType) {
      return false;
    }
    const fieldValue = getPostMetaFields(select, context)?.[args.key]?.value;
    if (fieldValue === void 0) {
      return false;
    }
    const areCustomFieldsEnabled = select(import_store.store).getEditorSettings().enableCustomFields;
    if (areCustomFieldsEnabled) {
      return false;
    }
    const canUserEdit = select(import_core_data.store).canUser("update", {
      kind: "postType",
      name: context?.postType,
      id: context?.postId
    });
    if (!canUserEdit) {
      return false;
    }
    return true;
  },
  getFieldsList({ select, context }) {
    return getPostMetaFields(select, context);
  },
  editorUI({ select, context }) {
    const metaFields = Object.entries(
      getPostMetaFields(select, context) || {}
    ).map(([key, field]) => ({
      label: field.label,
      args: {
        key
      },
      type: field.type
    }));
    return {
      mode: "dropdown",
      data: metaFields
    };
  }
};
//# sourceMappingURL=post-meta.js.map
