"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_data_exports = {};
__export(post_data_exports, {
  default: () => post_data_default
});
module.exports = __toCommonJS(post_data_exports);
var import_i18n = require("@wordpress/i18n");
var import_core_data = require("@wordpress/core-data");
var import_block_editor = require("@wordpress/block-editor");
const NAVIGATION_BLOCK_TYPES = [
  "core/navigation-link",
  "core/navigation-submenu"
];
function getPostDataFields(select, context, clientId) {
  const { getEditedEntityRecord } = select(import_core_data.store);
  const { getBlockAttributes, getBlockName } = select(import_block_editor.store);
  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: (0, import_i18n.__)("Post Date"),
        value: entityDataValues?.date,
        type: "string"
      },
      modified: {
        label: (0, import_i18n.__)("Post Modified Date"),
        value: entityDataValues?.modified,
        type: "string"
      },
      link: {
        label: (0, import_i18n.__)("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(import_block_editor.store);
    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(import_core_data.store).editEntityRecord(
      "postType",
      context?.postType,
      context?.postId,
      newData
    );
  },
  canUserEditValue({ select, context, args }) {
    const { getBlockName, getSelectedBlockClientId } = select(import_block_editor.store);
    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(import_core_data.store).canUser("update", {
      kind: "postType",
      name: context?.postType,
      id: context?.postId
    });
    if (!canUserEdit) {
      return false;
    }
    return true;
  },
  getFieldsList({ select, context }) {
    const clientId = select(import_block_editor.store).getSelectedBlockClientId();
    return getPostDataFields(select, context, clientId);
  },
  editorUI({ select, context }) {
    const selectedBlock = select(import_block_editor.store).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
    };
  }
};
//# sourceMappingURL=post-data.js.map
