<?
  // TODO: This file needs love
  $topFacetSet = $this->recommend->getTopFacetSet();
  $topFacetSettings = $this->recommend->getTopFacetSettings();
  $results = $this->recommend->getResults();
?>
<? if (isset($topFacetSet)): ?>
  <? foreach($topFacetSet as $title => $cluster): ?>
    <? $moreClass = ' NarrowGroupHidden_'.$this->escapeHtml($title).' hidden'; ?>
    <? $allowExclude = $this->recommend->excludeAllowed($title); ?>
    <div class="top-facets">
      <? $limit = $topFacetSettings['rows'] * $topFacetSettings['cols']; ?>
      <? foreach($cluster['list'] as $index => $thisFacet): ?>
        <span class="facet <?=$index > $limit ? $moreClass : '' ?>">
          <? if ($thisFacet['isApplied']):
            if (isset($thisFacet['specialType']) && $thisFacet['specialType'] == 'keyword') {
              $removeLink = $this->currentPath().$results->getUrlQuery()->replaceTerm($thisFacet['value'], '');
            } else {
              $removeLink = $this->currentPath().$results->getUrlQuery()->removeFacet($title, $thisFacet['value'], $thisFacet['operator']);
            } ?>
            <a href="<?=$removeLink ?>" class="applied">
              <?=$this->escapeHtml($thisFacet['displayText'])?> <i class="fa fa-check" aria-hidden="true"></i><!--
         --></a>
          <? else: ?>
              <span class="badge"><?=$this->localizedNumber($thisFacet['count']) ?>
                  <? if ($allowExclude): ?>
                      <a href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'], 'NOT')?>" title="<?=$this->transEsc('exclude_facet')?>"><i class="fa fa-times" aria-hidden="true"></i></a>
                  <? endif; ?>
            </span>
            <a href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'], $thisFacet['operator'])?>">
              <?=$this->escapeHtml($thisFacet['displayText'])?><!--
         --></a>
          <? endif; ?>
        </span>
        <? /* More link */ ?>
        <? if ($index == $limit): ?>
          <span id="more-NarrowGroupHidden_<?=$this->escapeHtml($title)?>" class="narrow-toggle">
            <a href="#" onclick="moreFacets('NarrowGroupHidden_<?=$this->escapeHtml($title)?>'); return false;">
              <?=$this->transEsc('more') ?> ...
            </a>
          </span>
        <? endif; ?>
      <? endforeach; ?>
      <? if (count($cluster['list']) > $limit): ?>
        <a class="narrow-toggle <?=$moreClass ?>" href="#" onclick="lessFacets('NarrowGroupHidden_<?=$title ?>'); return false;">
          <?=$this->transEsc('less') ?> ...
        </a>
      <? endif; ?>
    </div>
  <? endforeach; ?>
<? endif; ?>
