// packages/editor/src/bindings/term-data.js
import { __ } from "@wordpress/i18n";
import { store as coreDataStore } from "@wordpress/core-data";
import { store as blockEditorStore } from "@wordpress/block-editor";
var NAVIGATION_BLOCK_TYPES = [
  "core/navigation-link",
  "core/navigation-submenu"
];
function createDataFields(termDataValues, idValue) {
  return {
    id: {
      label: __("Term ID"),
      value: idValue,
      type: "string"
    },
    name: {
      label: __("Name"),
      value: termDataValues?.name,
      type: "string"
    },
    slug: {
      label: __("Slug"),
      value: termDataValues?.slug,
      type: "string"
    },
    link: {
      label: __("Link"),
      value: termDataValues?.link,
      type: "string"
    },
    description: {
      label: __("Description"),
      value: termDataValues?.description,
      type: "string"
    },
    parent: {
      label: __("Parent ID"),
      value: termDataValues?.parent,
      type: "string"
    },
    count: {
      label: __("Count"),
      value: `(${termDataValues?.count ?? 0})`,
      type: "string"
    }
  };
}
function getTermDataFields(select, context, clientId) {
  const { getEntityRecord } = select(coreDataStore);
  const { getBlockAttributes, getBlockName } = select(blockEditorStore);
  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(blockEditorStore);
    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(blockEditorStore).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 }
    }));
  }
};
export {
  term_data_default as default
};
//# sourceMappingURL=term-data.js.map
