<?php

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

use Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\Post\Editors\Editors;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Str;
class Avia extends Editors
{
    const META_KEY = '_aviaLayoutBuilderCleanData';
    private $extracted_data;
    public function __construct($post_id)
    {
        $this->post_id = $post_id;
        $this->content = get_post_meta($post_id, self::META_KEY, \true);
    }
    public static function is_active($post_id): bool
    {
        return get_post_meta($post_id, '_aviaLayoutBuilder_active', \true) === 'active';
    }
    public function get_content($post_id): string
    {
        $pattern = '/\[(\w+)\s*([^]]*?)\]\s*([^[]*?)\s*\[\/\1\]/';
        $this->extracted_data = '';
        $found = preg_match_all($pattern, $this->content, $matches, \PREG_SET_ORDER);
        if (!$found) {
            $this->extracted_data = $this->content;
        }
        foreach ($matches as $match) {
            if (in_array(trim($match[1]), DefaultEditor::SUPPORTED_SHORTCODES)) {
                if (isset($match[3])) {
                    $this->extracted_data .= $match[3] . "\n";
                }
            }
        }
        return $this->extracted_data;
    }
    public function add_link($source, $phrase, $phrase_replacement): bool
    {
        // insert link into content
        $content = self::insert_link_in_content($this->content, $source, $phrase, $phrase_replacement);
        if ($content === \false) {
            return \false;
        }
        $this->content = $content;
        $this->has_unsaved_changes = \true;
        return \true;
    }
    public function update_post($suggestions = null): bool
    {
        if ($this->has_unsaved_changes) {
            $success = update_post_meta($this->post_id, self::META_KEY, $this->content) !== \false;
            if ($success) {
                $this->has_unsaved_changes = \false;
                // sync with wp post content
                $this->after_update_post($suggestions);
                return \true;
            }
        }
        return \false;
    }
    public function remove_link(string $link, string $anchor, $save = \true): bool
    {
        $pattern = '/<a\b[^>]*href="' . preg_quote($link, '/') . '"[^>]*>' . preg_quote($anchor, '/') . '<\/a>/i';
        // replace an occurrence of the link
        $content = preg_replace($pattern, $anchor, $this->content);
        if ($this->content === $content || is_null($content)) {
            return \false;
        }
        $this->content = $content;
        $this->has_unsaved_changes = \true;
        if ($save) {
            return $this->update_post();
        }
        return \true;
    }
    public function replace_phrase(string $link, string $phrase, string $replacement, bool $save): bool
    {
        $pattern = '/<a\b[^>]*href="' . preg_quote($link, '/') . '"[^>]*>' . preg_quote($phrase, '/') . '<\/a>/i';
        // replace an occurrence of the link
        $updated_content = preg_replace_callback($pattern, function ($match) use ($link, $replacement) {
            return str_replace($link, $replacement, $match[0]);
        }, $this->content);
        if ($this->content === $updated_content || is_null($updated_content)) {
            return \false;
        }
        $this->content = $updated_content;
        $this->has_unsaved_changes = \true;
        if ($save) {
            return $this->update_post();
        }
        return \true;
    }
}
