<?php
// Silence may be golden, but sometimes words paint the canvas of understanding.

namespace Plugli\Linksy\SearchAndReplace\Admin\Partials\SearchAndReplace\Helpers;

use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Database\Database;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Str;

use Plugli\Linksy\SearchAndReplace\Helpers\Post\Post;

class TitleReplace extends Helpers
{
    public function get($search_mode, $filter_query, $options = [])
    {
        // 'trash', 'auto-draft'
        $posts = Database::table("posts")->select(['ID', 'post_title', 'post_name', 'post_type', 'post_status'])
            ->whereNotIn('post_status', ['inherit']);

        // perform database query based on search mode
        if ($search_mode === 'EXACT') {
            $posts = $posts->where('post_title', $this->search);
        } elseif ($search_mode === 'CONTAINS') {
            $posts = $posts->whereLike('post_title', $this->search);
        }

        // execute query
        $posts = $posts->get();
        $data = [];

        foreach ($posts as $post) {
            $key = $post->ID;

            $new_post = new Post($post->ID);

            $data[$key] = [
                'source' => $post->post_title,
                'post_id' => $post->ID,
                'link' => $new_post->get_link(),
                'post_type' => $post->post_type,
                'view_link' => $new_post->get_link(),
                'edit_link' => $new_post->get_edit_link(),
            ];
        }

        if (!empty($this->filters['search_mode'])) {
            return $this->filter($data);
        }

        return $data;
    }

    public function filter($data = [], $query = '')
    {
        if (empty($this->filters) || !isset($this->filters['search_mode']) || !is_array($this->filters['search_mode'])) {
            return $data;
        }

        $filtered_data = [];

        foreach ($data as $item) {
            $match = true;
            foreach ($this->filters['search_mode'] as $mode) {
                $query = null;
                $this->filter_mode = $mode;

                if ($mode === 'title' && isset($this->filters['title'])) {
                    $query = $this->filters['title'];
                } elseif (($mode === 'link' || $mode === 'link-is') && isset($this->filters['link'])) {
                    $query = $this->filters['link'];
                }

                if ($query !== null) {
                    if (!$this->filterByMode($item, $query)) {
                        $match = false;
                        break;
                    }
                }
            }

            if ($match) {
                $filtered_data[] = $item;
            }
        }

        return $filtered_data;
    }

    protected function filterByMode($item, $query)
    {
        if (empty($query)) {
            return true;
        }

        if ($this->filter_mode === 'link-is') {
            return isset($item['link']) && strtolower($item['link']) === strtolower($query);
        }

        $key = $this->filter_mode === 'title' ? 'source' : 'link';

        if (isset($item[$key])) {
            return Str::contains(strtolower($item[$key]), strtolower($query));
        }

        return false;
    }
}
