<?php

// Silence may be golden, but sometimes words paint the canvas of understanding.
namespace Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\Post\Traits;

use Exception;
use Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\Settings;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Arr;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\External\Encoding;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Str;
trait Suggestion
{
    public function get_suggestable_content($clean = \false, $tags_to_ignore = [])
    {
        $content = $this->get_content(\false);
        if (empty($content)) {
            return '';
        }
        // Prevent suggestions that have links
        $prevent_suggestions = \false;
        // Settings::get('disable_suggestions_of_sentences_with_links', true);
        if ($prevent_suggestions) {
            // Convert content to sentences
            $sentences = Str::text_to_paragraphs($content);
            // Filter out sentences containing <a> tags
            $filtered_sentences = [];
            foreach ($sentences as $sentence) {
                if (!preg_match('/<a\b[^>]*>(.*?)<\/a>|<a\b[^>]*>/i', $sentence)) {
                    $filtered_sentences[] = $sentence;
                }
            }
            // Convert back to text
            $content = implode("\n", $filtered_sentences);
        }
        // delete some tags
        $content = Str::delete_tags(Str::strip_comments($content), array_merge(['img', 'button', 'script', 'style', 'code', 'pre', 'cite', 'thead', 'th'], $tags_to_ignore));
        // to skip
        $no_type_to_skip = Settings::get('no_of_type_to_skip', 0);
        if ($no_type_to_skip) {
            $type_to_skip = Settings::get('type_to_skip', Settings::TYPES_TO_SKIP['WORDS']);
            if ($type_to_skip == Settings::TYPES_TO_SKIP['WORDS']) {
                $content = implode(" ", array_slice(explode(" ", $content), $no_type_to_skip - 1));
            } else if ($type_to_skip == Settings::TYPES_TO_SKIP['SENTENCES']) {
                $phrases = array_values(Str::text_to_sentences($content));
                $last_phrase = $phrases[min(count($phrases) - 1, $no_type_to_skip)];
                $last_phrase_pos = Str::position($content, $last_phrase);
                $content = $last_phrase_pos === \false ? '' : substr($content, $last_phrase_pos);
            } else if ($type_to_skip == Settings::TYPES_TO_SKIP['PARAGRAPHS']) {
                $paragraphs = Str::text_to_paragraphs($content);
                $last_paragraph = $paragraphs[min(count($paragraphs) - 1, $no_type_to_skip)];
                $last_paragraph_pos = Str::position($content, $last_paragraph);
                $content = $last_paragraph_pos === \false ? '' : substr($content, $last_paragraph_pos);
            }
        }
        if ($clean) {
            $content = preg_replace('/<br\s*\/?>/s', "\n", $content);
            $content = Encoding::toUTF8(Str::strip_emojis(Str::strip_tags($content)));
        }
        return $content;
    }
    public function is_suggestable(array $ignore_list = [], bool $throw = \false)
    {
        if (!$this->exists()) {
            if ($throw) {
                throw new Exception('Post does not exists');
            }
            return \false;
        }
        $available_settings = ['suggestions_ignored_posts', 'suggestions_ignored_categories', 'ignore_post_older_than', 'ignore_post_younger_than', 'max_outbound_links_per_post', 'max_inbound_links_per_post'];
        // fetch settings
        $settings = Settings::many(Arr::filter($available_settings, function ($s) use ($ignore_list) {
            return !in_array($s, $ignore_list);
        }));
        if (!empty($settings['suggestions_ignored_posts']) && $this->is_ignored($settings['suggestions_ignored_posts']) || get_post_meta($this->get_id(), 'linksy_suggestions_ignored_post', \true)) {
            return \false;
        }
        if (!empty($settings['suggestions_ignored_categories']) && $this->has_ignored_category($settings['suggestions_ignored_categories'])) {
            return \false;
        }
        if (!empty($settings['ignore_post_older_than']) && $this->is_older_than($settings['ignore_post_older_than'])) {
            return \false;
        }
        if (!empty($settings['ignore_post_younger_than']) && $this->is_younger_than($settings['ignore_post_younger_than'])) {
            return \false;
        }
        if (!empty($settings['max_inbound_links_per_post']) && $this->has_exceed_max_inbound_links()) {
            if ($throw) {
                throw new Exception("max inbound link value({$settings['max_inbound_links_per_post']}) reached", 1);
            }
            return \false;
        }
        if (!empty($settings['max_outbound_links_per_post']) && $this->has_exceed_max_outbound_links()) {
            if ($throw) {
                throw new Exception("max outbound link value({$settings['max_outbound_links_per_post']}) reached", 1);
            }
            return \false;
        }
        return \true;
    }
    // Related Linksy Settings Validators
    public function is_older_than($date)
    {
        return !empty($date) && $this->is_before(gmdate('Y-m-d', strtotime('-' . $date . ' months')));
    }
    public function is_younger_than($date)
    {
        return !empty($date) && $this->is_after(gmdate('Y-m-d', strtotime('-' . $date . ' months')));
    }
    public function is_ignored($ignored_list)
    {
        return !empty($ignored_list) && in_array($this->get_link(), $ignored_list);
    }
    public function has_ignored_category($ignored_categories)
    {
        return !empty($ignored_categories) && $this->has_category($ignored_categories);
    }
}
