import { __ } from "@wordpress/i18n";
import { store as coreDataStore } from "@wordpress/core-data";
import { store as blockEditorStore } from "@wordpress/block-editor";
const NAVIGATION_BLOCK_TYPES = [
  "core/navigation-link",
  "core/navigation-submenu"
];
function getPostDataFields(select, context, clientId) {
  const { getEditedEntityRecord } = select(coreDataStore);
  const { getBlockAttributes, getBlockName } = select(blockEditorStore);
  let entityDataValues, dataFields;
  const blockName = getBlockName?.(clientId);
  const isNavigationBlock = NAVIGATION_BLOCK_TYPES.includes(blockName);
  let postId, postType;
  if (isNavigationBlock) {
    const blockAttributes = getBlockAttributes?.(clientId);
    postId = blockAttributes?.id;
    postType = blockAttributes?.type;
  } else {
    postId = context?.postId;
    postType = context?.postType;
  }
  if (postType && postId) {
    entityDataValues = getEditedEntityRecord(
      "postType",
      postType,
      postId
    );
    dataFields = {
      date: {
        label: __("Post Date"),
        value: entityDataValues?.date,
        type: "string"
      },
      modified: {
        label: __("Post Modified Date"),
        value: entityDataValues?.modified,
        type: "string"
      },
      link: {
        label: __("Post Link"),
        value: entityDataValues?.link,
        type: "string"
      }
    };
  }
  if (!Object.keys(dataFields || {}).length) {
    return null;
  }
  return dataFields;
}
var post_data_default = {
  name: "core/post-data",
  getValues({ select, context, bindings, clientId }) {
    const dataFields = getPostDataFields(select, context, clientId);
    const newValues = {};
    for (const [attributeName, source] of Object.entries(bindings)) {
      const fieldKey = source.args.field;
      const { value: fieldValue, label: fieldLabel } = dataFields?.[fieldKey] || {};
      newValues[attributeName] = fieldValue ?? fieldLabel ?? fieldKey;
    }
    return newValues;
  },
  setValues({ dispatch, context, bindings, clientId, select }) {
    const { getBlockName } = select(blockEditorStore);
    const blockName = getBlockName?.(clientId);
    if (NAVIGATION_BLOCK_TYPES.includes(blockName)) {
      return false;
    }
    const newData = {};
    Object.values(bindings).forEach(({ args, newValue }) => {
      newData[args.field] = newValue;
    });
    dispatch(coreDataStore).editEntityRecord(
      "postType",
      context?.postType,
      context?.postId,
      newData
    );
  },
  canUserEditValue({ select, context, args }) {
    const { getBlockName, getSelectedBlockClientId } = select(blockEditorStore);
    const clientId = getSelectedBlockClientId();
    const blockName = getBlockName?.(clientId);
    if (NAVIGATION_BLOCK_TYPES.includes(blockName)) {
      return false;
    }
    if (context?.query || context?.queryId) {
      return false;
    }
    if (!context?.postType) {
      return false;
    }
    const fieldValue = getPostDataFields(select, context, void 0)?.[args.field]?.value;
    if (fieldValue === void 0) {
      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 }) {
    const clientId = select(blockEditorStore).getSelectedBlockClientId();
    return getPostDataFields(select, context, clientId);
  },
  editorUI({ select, context }) {
    const selectedBlock = select(blockEditorStore).getSelectedBlock();
    if (selectedBlock?.name !== "core/post-date") {
      return {};
    }
    if (NAVIGATION_BLOCK_TYPES.includes(selectedBlock?.name)) {
      return {};
    }
    const postDataFields = Object.entries(
      getPostDataFields(select, context) || {}
    ).map(([key, field]) => ({
      label: field.label,
      args: {
        field: key
      },
      type: field.type
    }));
    return {
      mode: "dropdown",
      data: postDataFields
    };
  }
};
export {
  post_data_default as default
};
//# sourceMappingURL=post-data.js.map
