<?php

namespace Formatrica\Capability\Rules;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Formatrica\Capability\Capabilities;
use Formatrica\Capability\Premium_Config_Rule_Interface;

/**
 * Save-path gate for the per-form wrapper CSS class (forms.wrapper_class).
 *
 * Freezes options.wrapper_class on Free (preserve-existing): a new class can't be
 * set and a grandfathered one can't be changed. The renderer that outputs the
 * class lives in CORE, so a lapsed form keeps its wrapper class.
 */
final class Wrapper_Class_Rule implements Premium_Config_Rule_Interface
{
    public function capability(): string
    {
        return Capabilities::FORMS_WRAPPER_CLASS;
    }

    public function enforce(array &$config, array $existing): array
    {
        $rejected = [];

        $incoming = isset($config['options']['wrapper_class']) ? (string) $config['options']['wrapper_class'] : '';
        $existing_value = isset($existing['options']['wrapper_class']) ? (string) $existing['options']['wrapper_class'] : '';

        if ($incoming === $existing_value) {
            return $rejected;
        }

        $config['options']['wrapper_class'] = $existing_value;
        $rejected[] = [
            'key' => 'options.wrapper_class',
            'reason' => __('A custom form-wrapper class is a Pro feature; reverted.', 'formatrica'),
            'capability' => $this->capability(),
            'feature' => 'wrapper_class',
        ];

        return $rejected;
    }
}
