<?php

/**
 * Ahead WebSoft Technologies Pvt Ltd.
 * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
 * @license    https://www.socialengine.com/policies
 */

class Core_View_Helper_SavecollectionIcon extends Zend_View_Helper_Abstract
{
	/**
	 * Render the save-to-collection bookmark icon for a given content item.
	 * Returns empty string if feature is disabled, viewer is not authenticated,
	 * or content type is not supported.
	 *
	 * @param Core_Model_Item_Abstract|null $item
	 * @return string HTML markup for the save icon
	 */
	public function savecollectionIcon($item)
	{
		if (!$item || !($item instanceof Core_Model_Item_Abstract)) {
			return '';
		}

		// Check if feature is enabled
		$api = Engine_Api::_()->getApi('savecollection', 'core');
		if (!$api->isEnabled()) {
			return '';
		}

		// Check viewer authentication
		$viewer = Engine_Api::_()->user()->getViewer();
		if (!$viewer || !$viewer->getIdentity()) {
			return '';
		}

		// Check if content type is supported
		$contentType = $item->getType();
		// if (!in_array($contentType, $api->getSupportedContentTypes())) {
		//   return '';
		// }

		$contentId = $item->getIdentity();
		$isSaved = $api->getSavedItem($viewer, $contentType, $contentId) !== null;

		// Render the icon inline
		$uniqueId = 'savecollection_icon_' . $contentType . '_' . $contentId;
		$iconClass = 'fa-regular fa-bookmark';
		$stateClass = $isSaved ? 'savecollection_icon_saved' : 'savecollection_icon_unsaved';
		$ariaLabel = $isSaved ? $this->view->translate('Remove from Saved') : $this->view->translate('Save');

		$html = '<a href="javascript:void(0);" id="' . $uniqueId . '" '
			. 'class="savecollection_icon ' . $stateClass . '" '
			. 'data-content-type="' . htmlspecialchars($contentType, ENT_QUOTES, 'UTF-8') . '" '
			. 'data-content-id="' . (int) $contentId . '" '
			. 'data-saved="' . ($isSaved ? '1' : '0') . '" '
			. 'title="' . htmlspecialchars($ariaLabel, ENT_QUOTES, 'UTF-8') . '" '
			. 'aria-label="' . htmlspecialchars($ariaLabel, ENT_QUOTES, 'UTF-8') . '" '
			. 'role="button">'
			. '<i data-bs-toggle="tooltip" data-bs-original-title="' . $ariaLabel . '" title="' . $ariaLabel . '" class="' . $iconClass . '"></i>'
			. '</a>';

		return $html;
	}
}
