<?php

namespace Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\Post\Traits;

use Linksy\SearchAndReplace\Plugli\Standard\Helpers\External\Encoding;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Str;
trait Editor
{
    public $supported_editiors = ['Elementor', 'Thrive', 'Cornerstone', 'Oxygen', 'Beaver', 'Themify', 'Bricks', 'Muffin', 'Avia', 'Breakdance', 'DefaultEditor'];
    public function get_editor($by_name = \false)
    {
        if (!$this->editor) {
            foreach ($this->supported_editiors as $editor) {
                $editor_class = $this->editor_namespace . "\\{$editor}";
                if (class_exists($editor_class) && method_exists($editor_class, 'is_active')) {
                    if ($editor_class::is_active($this->get_ID())) {
                        $this->editor = new $editor_class($this->get_ID());
                        break;
                    }
                }
            }
        }
        return $by_name ? $this->editor->get_name() : $this->editor;
    }
    public function get_editor_name()
    {
        return $this->get_editor(\true);
    }
    public function get_word_cnt()
    {
        return $this->get_editor()->get_word_cnt();
    }
    public function get_content($clean = \false)
    {
        $content = $this->get_editor()->get_content($this->get_ID());
        if ($clean && $content) {
            $content = preg_replace('/<br\s*\/?>/s', "\n", $content);
            $content = Encoding::toUTF8(Str::strip_emojis(Str::strip_tags(Str::strip_comments($content))));
        }
        return Str::indent_dom($content);
    }
    public function apply_suggestions($suggestions)
    {
        $results = ['failed' => [], 'processed' => []];
        // initialize the active editor
        if ($this->get_editor()) {
            foreach ($suggestions as $suggestion) {
                $status_data = $this->update_content(Str::protrude_dom($suggestion['source']), $suggestion['phrase'], $suggestion['replacement']);
                // add post title
                if (isset($suggestion['post_title'])) {
                    $status_data['title'] = $suggestion['post_title'];
                }
                if (isset($status_data['errors'])) {
                    $results['failed'][] = $status_data;
                } else {
                    $results['processed'][] = $status_data;
                }
            }
            // persist the updates to the database
            $this->get_editor()->update_post($suggestions);
            $this->refresh_links();
            $this->delete_broken_links();
        }
        return $results;
    }
    public function insert_link($content, $source, $phrase, $replacement)
    {
        return $this->get_editor()->insert_link_in_content($content, Str::protrude_dom($source), $phrase, $replacement);
    }
    public function save_post($title, $source, $phrase, $phrase_replacement)
    {
        $success = \false;
        if ($this->get_editor()) {
            $source = Str::protrude_dom($source);
            // add link to post content
            $status_data = $this->update_content($source, $phrase, $phrase_replacement);
            // add post title to status data
            $status_data['title'] = $title;
            // check errors
            if (!isset($status_data['errors'])) {
                $this->editor->update_post([['source' => Str::protrude_dom($source), 'phrase' => $phrase, 'replacement' => $phrase_replacement]]);
                $success = \true;
                $this->refresh_links();
                $this->delete_broken_links();
            }
        }
        return [$success, $status_data];
    }
    private function update_content($source, $phrase, $phrase_replacement)
    {
        $status_data = ['id' => $this->get_ID(), 'phrase' => $phrase];
        $sentence = Str::protrude_dom($source);
        // apply suggestion to post content
        $success = $this->editor->add_link($sentence, $phrase, $phrase_replacement);
        if (!$success) {
            $status_data['errors'] = ['Could not find "' . $sentence . '" in this post'];
        }
        return $status_data;
    }
    public function replace_anchors(array $anchors, $replace = \false)
    {
        $stats = ['replaced' => [], 'failed' => []];
        if (!$this->get_editor() || empty($anchors)) {
            return $stats;
        }
        foreach ($anchors as $anchor) {
            $anchor = (object) $anchor;
            if ($replace) {
                $success = $this->editor->replace_phrase($anchor->view_url, $anchor->anchor, $anchor->anchor_replacement, \true);
            } else {
                $success = \false;
            }
            if (!empty($stats)) {
                $success ? $stats['replaced'][] = $anchor->anchor : $stats['failed'][] = $anchor->anchor;
            }
        }
        return $stats;
    }
}
