"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);

// packages/editor/src/bindings/term-data.js
var term_data_exports = {};
__export(term_data_exports, {
  default: () => term_data_default
});
module.exports = __toCommonJS(term_data_exports);
var import_i18n = require("@wordpress/i18n");
var import_core_data = require("@wordpress/core-data");
var import_block_editor = require("@wordpress/block-editor");
var NAVIGATION_BLOCK_TYPES = [
  "core/navigation-link",
  "core/navigation-submenu"
];
function createDataFields(termDataValues, idValue) {
  return {
    id: {
      label: (0, import_i18n.__)("Term ID"),
      value: idValue,
      type: "string"
    },
    name: {
      label: (0, import_i18n.__)("Name"),
      value: termDataValues?.name,
      type: "string"
    },
    slug: {
      label: (0, import_i18n.__)("Slug"),
      value: termDataValues?.slug,
      type: "string"
    },
    link: {
      label: (0, import_i18n.__)("Link"),
      value: termDataValues?.link,
      type: "string"
    },
    description: {
      label: (0, import_i18n.__)("Description"),
      value: termDataValues?.description,
      type: "string"
    },
    parent: {
      label: (0, import_i18n.__)("Parent ID"),
      value: termDataValues?.parent,
      type: "string"
    },
    count: {
      label: (0, import_i18n.__)("Count"),
      value: `(${termDataValues?.count ?? 0})`,
      type: "string"
    }
  };
}
function getTermDataFields(select, context, clientId) {
  const { getEntityRecord } = select(import_core_data.store);
  const { getBlockAttributes, getBlockName } = select(import_block_editor.store);
  let termDataValues, dataFields;
  const blockName = getBlockName?.(clientId);
  const isNavigationBlock = NAVIGATION_BLOCK_TYPES.includes(blockName);
  let termId, taxonomy;
  if (isNavigationBlock) {
    const blockAttributes = getBlockAttributes?.(clientId);
    termId = blockAttributes?.id;
    const typeFromAttributes = blockAttributes?.type;
    taxonomy = typeFromAttributes === "tag" ? "post_tag" : typeFromAttributes;
  } else {
    termId = context?.termId;
    taxonomy = context?.taxonomy;
  }
  if (taxonomy && termId) {
    termDataValues = getEntityRecord("taxonomy", taxonomy, termId);
    if (!termDataValues && context?.termData) {
      termDataValues = context.termData;
    }
    if (termDataValues) {
      dataFields = createDataFields(termDataValues, termId);
    }
  } else if (context?.termData) {
    termDataValues = context.termData;
    dataFields = createDataFields(
      termDataValues,
      termDataValues?.term_id
    );
  }
  if (!dataFields || !Object.keys(dataFields).length) {
    return null;
  }
  return dataFields;
}
var term_data_default = {
  name: "core/term-data",
  usesContext: ["taxonomy", "termId", "termData"],
  getValues({ select, context, bindings, clientId }) {
    const dataFields = getTermDataFields(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;
  },
  // eslint-disable-next-line no-unused-vars
  setValues({ dispatch, context, bindings }) {
    return false;
  },
  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?.termQuery) {
      return false;
    }
    if (!context?.taxonomy || !context?.termId) {
      return false;
    }
    const fieldValue = getTermDataFields(select, context, void 0)?.[args.field]?.value;
    if (fieldValue === void 0) {
      return false;
    }
    return false;
  },
  getFieldsList({ select, context }) {
    const clientId = select(import_block_editor.store).getSelectedBlockClientId();
    const termDataFields = getTermDataFields(select, context, clientId);
    if (!termDataFields) {
      return [];
    }
    return Object.entries(termDataFields).map(([key, field]) => ({
      label: field.label,
      type: field.type,
      args: { field: key }
    }));
  }
};
//# sourceMappingURL=term-data.js.map
