<?php

namespace Miruni\BlockWidget;
use Miruni\Miruni_Preview_Meta_Keys;

if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

class Miruni_Block_Widget
{
    public string $id;
    public string $type;
    public string $title;
    public string $content;
    /**
     * Summary of sidebar_locations
     * @var array<mixed>
     */
    public array $sidebar_locations;
    /**
     * Summary of raw_data
     * @var array<mixed>
     */
    public array $raw_data;

    /**
     * Miruni_Block_Widget constructor.
     *
     * @param string $id The widget ID.
     * @param string $type The widget type (e.g., 'custom_html', 'block').
     * @param string $title The widget title.
     * @param string $content The widget content.
     * @param array<mixed> $sidebar_locations Array of sidebar locations where the widget is placed.
     * @param array<mixed> $raw_data Raw data of the widget.
     */
    public function __construct(string $id, string $type, string $title, string $content, array $sidebar_locations, array $raw_data)
    {
        $this->id = $id;
        $this->type = $type;
        $this->title = $title;
        $this->content = $content;
        $this->sidebar_locations = $sidebar_locations;
        $this->raw_data = $raw_data;
    }

    /**
     * Convert the widget data to an array format.
     *
     * @return array<mixed> The widget data as an associative array.
     */
    public function to_array(): array
    {
        return [
            'id' => $this->id,
            'type' => $this->type,
            'title' => $this->title,
            'content' => $this->content,
            'sidebar_locations' => $this->sidebar_locations,
            'raw_data' => $this->raw_data,
        ];
    }

    public function matches_option_and_id(string $option, string $id): bool
    {
        // id is a string like "xxxxblock-1" or "xxxxcustom_html-1"
        // check that the id ends with "{$option}-{$id}"
        // and that the type is the same as $option
        return $this->type === $option && str_ends_with($this->id, "{$option}-{$id}");
    }
}
