<?php

namespace App\Support;

class AttributeValue
{
    /**
     * Normalise a submitted attribute value into a storable scalar.
     *
     * Dynamic-source select/checkbox options arrive as objects (e.g. {id,name,slug,email});
     * the attribute value column is a string, so reduce an array to its name (preferred), id,
     * or a JSON fallback. Scalars pass through unchanged.
     */
    public static function normalise(mixed $value): mixed
    {
        if (is_array($value)) {
            return $value['name'] ?? $value['id'] ?? json_encode($value);
        }

        return $value;
    }
}
