<?php

namespace MenuManagerUltra\lib\Term;

class TermDataFormatter {

  public static function normalizeTypeData($type, $apply_to_obj = null) {

    if (!$apply_to_obj) {
      $apply_to_obj = $type;
    }

    return $type;
  }

  public static function normalizeTermData($term, $apply_to_obj = null) {

    if (is_numeric($term)) {
      $term = get_term($term);
    }

    if (!$apply_to_obj) {
      $apply_to_obj = $term;
    }

    $apply_to_obj->ID = $term->term_id;
    $apply_to_obj->display_title = $term->name;
    $apply_to_obj->is_taxonomy = true;
    $apply_to_obj->type_slug = $term->taxonomy;

    return static::applyLinks($term, $apply_to_obj);
  }

  public static function applyLinks($term, $apply_to_obj = null) {

    if (is_numeric($term)) {
      $term = get_term($term);
    }

    if (!$term) {
      throw new \InvalidArgumentException("Invalid Term");
    }
  
    if (!$apply_to_obj) {
      $apply_to_obj = $term;
    }

    $apply_to_obj->permalink = get_term_link($term);
    $apply_to_obj->edit_link = html_entity_decode(get_edit_term_link($term));
    $apply_to_obj->display_link = wp_make_link_relative($apply_to_obj->permalink);
    $apply_to_obj->goto_link = $apply_to_obj->permalink;
  
    return $apply_to_obj;
  }
  

}