<?php

namespace Mtc\Plugins\BlockManager\Services;

/**
 * Inverse of VariantLessGenerator's header emitter.
 *
 * Extracts the `/* @designer-meta { JSON } *\/` header from a LESS string
 * and returns the decoded JSON object. The header carries a full snapshot
 * of the property tree at publish time, so the Designer can re-open a
 * published variant without re-deriving from the CSS body.
 *
 * Returns null when:
 *   - the header is missing (variant was hand-authored / not Designer-published)
 *   - the comment matches but the JSON body fails to decode
 */
class VariantLessParser
{
    /**
     * @return array<string, mixed>|null
     */
    public function parse(string $lessContent): ?array
    {
        if (!preg_match('#^\s*/\*\s*@designer-meta\s+(\{.*?\})\s*\*/#s', $lessContent, $matches)) {
            return null;
        }

        $decoded = json_decode($matches[1], true);
        return is_array($decoded) ? $decoded : null;
    }
}
