<?php
/**
 * @package         Quick Index
 * @version         3.4.0PRO
 * 
 * @author          Peter van Westen <info@regularlabs.com>
 * @link            http://regularlabs.com
 * @copyright       Copyright © 2023 Regular Labs All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Language\Text as JText;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Extension as RL_Extension;
use RegularLabs\Library\Protect as RL_Protect;
use RegularLabs\Library\RegEx as RL_RegEx;
use RegularLabs\Library\SystemPlugin as RL_SystemPlugin;
use RegularLabs\Plugin\System\QuickIndex\Params;
use RegularLabs\Plugin\System\QuickIndex\Replace;

// Do not instantiate plugin on install pages
// to prevent installation/update breaking because of potential breaking changes
$input = JFactory::getApplication()->input;
if (in_array($input->get('option', ''), ['com_installer', 'com_regularlabsmanager']) && $input->get('action', '') != '')
{
    return;
}

if ( ! is_file(JPATH_LIBRARIES . '/regularlabs/regularlabs.xml')
    || ! is_file(JPATH_LIBRARIES . '/regularlabs/src/SystemPlugin.php')
    || ! is_file(JPATH_LIBRARIES . '/regularlabs/src/DownloadKey.php')
)
{
    JFactory::getLanguage()->load('plg_system_quickindex', __DIR__);
    JFactory::getApplication()->enqueueMessage(
        JText::sprintf('QI_EXTENSION_CAN_NOT_FUNCTION', JText::_('QUICKINDEX'))
        . ' ' . JText::_('QI_REGULAR_LABS_LIBRARY_NOT_INSTALLED'),
        'error'
    );

    return;
}

if ( ! RL_Document::isJoomlaVersion(4, 'QUICKINDEX'))
{
    RL_Extension::disable('quickindex', 'plugin');

    RL_Document::adminError(
        JText::sprintf('RL_PLUGIN_HAS_BEEN_DISABLED', JText::_('QUICKINDEX'))
    );

    return;
}

if (true)
{
    class PlgSystemQuickIndex extends RL_SystemPlugin
    {
        public $_lang_prefix           = 'QI';
        public $_has_tags              = true;
        public $_disable_on_components = true;
        public $_jversion              = 3;

        protected function handleOnContentPrepare($area, $context, &$article, &$params, $page = 0)
        {
            Replace::addAutoIndex($area, $context, $article);

            return true;
        }

        public function processArticle(&$string, $area = 'article', $context = '', $article = null, $page = 0)
        {
            Replace::replaceTags($string, $area, $context, $page);

            Replace::moveAutoIndexToPage($string, $page);
        }

        protected function changeDocumentBuffer(&$buffer)
        {
            return Replace::replaceTags($buffer, 'component');
        }

        protected function changeModulePositionOutput(&$buffer, &$params)
        {
            Replace::replaceTags($buffer, 'modules');

            return true;
        }

        protected function changeFinalHtmlOutput(&$html)
        {
            Replace::replaceTags($html, 'html');

            return true;
        }

        protected function cleanFinalHtmlOutput(&$html)
        {
            $params = Params::get();

            RL_Protect::unprotect($html);

            RL_Protect::protectFields($html, Params::getTags(true, true));
            self::removeNoIndexTags($html);
            RL_Protect::unprotect($html);

            RL_Protect::removeInlineComments($html, 'Quick Index');

            if ( ! $params->place_comments)
            {
                RL_Protect::removeCommentTags($html, 'Quick Index');
            }

            $html = str_replace('<!-- QI: START -->', '', $html);
        }

        private static function removeNoIndexTags(&$html)
        {
            $regex = Params::getRegexNoIndex();

            $html = RL_RegEx::replace($regex, '\1', $html);
        }
    }
}
