<?php

namespace App\Campaigns\Manager;

/**
 * A settings array in one stable shape, so equal values compare equal.
 *
 * Associative keys are sorted at every depth; lists keep the order the
 * platform gave them, because for some fields (keyword lists, placements) the
 * order is the platform's own and a reorder is itself worth noticing.
 */
final class Canonical
{
    /**
     * @param  array<mixed>  $value
     * @return array<mixed>
     */
    public static function sort(array $value): array
    {
        foreach ($value as $key => $item) {
            if (is_array($item)) {
                $value[$key] = self::sort($item);
            }
        }

        if (! array_is_list($value)) {
            ksort($value);
        }

        return $value;
    }
}
