<?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
 */

namespace RegularLabs\Plugin\System\QuickIndex;

defined('_JEXEC') or die;

use RegularLabs\Library\Alias as RL_Alias;
use RegularLabs\Library\ArrayHelper as RL_Array;
use RegularLabs\Library\HtmlTag as RL_HtmlTag;
use RegularLabs\Library\RegEx as RL_RegEx;

class Heading
{
    var $attributes = [];
    var $id;
    var $level      = 1;
    var $match;
    var $number;
    var $settings;
    var $text;

    public function __construct(Settings $settings)
    {
        $this->settings = $settings;
    }

    public function getId($titles = null)
    {
        $id = RL_Alias::get(implode('-', $titles));
        $id = $this->removeWordsFromId($id);

        $level_number = $this->getLevelNumber();
        if ($this->settings->get('add_number_to_ids') && $level_number)
        {
            $id = $level_number . '-' . $id;
        }

        if ($this->settings->get('prefix_numeric_ids') && is_numeric($id[0]))
        {
            $id = $this->settings->get('prefix_numeric_ids_prefix') . $id;
        }

        if ($this->settings->get('prefix_ids'))
        {
            $id = $this->settings->get('prefix_ids_prefix') . $id;
        }

        return $id;
    }

    public function getLevelNumber()
    {
        $number = [];

        foreach ($this->number as $level => $level_number)
        {
            if ( ! $level_number)
            {
                continue;
            }

            $number[] = $this->settings->getLevelDigit($level, $level_number);
        }

        return implode('.', $number);
    }

    public function hasClass($classes)
    {
        if (empty($this->attributes['class']))
        {
            return false;
        }

        $intersect = array_intersect(explode(' ', $this->attributes['class']), $classes);

        return ! empty($intersect);
    }

    public function isInHeadingLevels($heading)
    {
        return $heading->level >= $this->settings->min_headings_level && $heading->level <= $this->settings->max_headings_level;
    }

    public function isInIndexLevels()
    {
        return $this->level >= $this->settings->min_index_level && $this->level <= $this->settings->max_index_level;
    }

    public function isTitle()
    {
        return $this->hasClass($this->settings->get('title_classes'));
    }

    public function setData($data)
    {
        foreach ($data as $key => $value)
        {
            $this->{$key} = $value;
        }

        $this->attributes = RL_HtmlTag::combineAttributes($data->attributes, ['class' => $this->settings->get('class_headings')], false);

        return $this;
    }

    private function removeWordsFromId($string)
    {
        $remove_words = $this->settings->get('remove_words');

        if (empty($remove_words))
        {
            return $string;
        }

        $remove_words = RL_RegEx::quote(RL_Array::toArray($remove_words));

        // Too heavy/complex to do in one regex
        $string = RL_RegEx::replace('^(' . $remove_words . '-)+', '', $string);
        $string = RL_RegEx::replace('(-' . $remove_words . ')+$', '', $string);
        $string = RL_RegEx::replace('(-' . $remove_words . ')+-', '-', $string);

        return $string;
    }
}
