/**
 * Resolve the final href for the <a> wrapper that wraps the bodycopy element from OUTSIDE.
 * Returns null when no link should be applied.
 *
 * @param {object} params
 * @param {object} params.attributes - Block attributes.
 * @param {string} params.postLink   - Resolved post permalink.
 * @param {string} params.authorLink - Resolved author archive URL.
 * @param {string} params.termLink   - Resolved term archive URL.
 * @returns {string|null}
 */
export function resolveBodycopyLink({ attributes, postLink, authorLink, termLink }) {
  const { customLink, dynamicTag, linkOption = 'none', selectedTag = 'post_excerpt' } = attributes;

  if (customLink) return customLink;
  if (!dynamicTag || linkOption === 'none') return null;

  const postTags = [
    'post_excerpt', 'post_date', 'post_meta', 'post_permalink',
    'term_list', 'acf_field', 'post_title',
  ];

  if (postTags.includes(selectedTag)) {
    if (linkOption === 'post' && postLink && postLink !== '#') return postLink;
  }

  if (selectedTag === 'comments_count' || selectedTag === 'comments_url') {
    if (linkOption === 'post' && postLink && postLink !== '#') return `${postLink}#comments`;
  }

  if (selectedTag === 'post_author') {
    if (linkOption === 'post'   && postLink   && postLink   !== '#') return postLink;
    if (linkOption === 'author' && authorLink && authorLink !== '#') return authorLink;
  }

  if (['term_name', 'term_description', 'term_meta'].includes(selectedTag)) {
    if (linkOption === 'term' && termLink && termLink !== '#') return termLink;
  }

  return null;
}
