import { store as coreDataStore } from "@wordpress/core-data";
import { store as editorStore } from "../store";
import { unlock } from "../lock-unlock";
function getPostMetaFields(select, context) {
  const { getEditedEntityRecord } = select(coreDataStore);
  const { getRegisteredPostMeta } = unlock(select(coreDataStore));
  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(coreDataStore).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(editorStore).getEditorSettings().enableCustomFields;
    if (areCustomFieldsEnabled) {
      return false;
    }
    const canUserEdit = select(coreDataStore).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
    };
  }
};
export {
  post_meta_default as default
};
//# sourceMappingURL=post-meta.js.map
