<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer;

use PpPhp\Core\Platform\Identity\PlatformIdGenerator;

final class DesignerIdGenerator
{
    public static function normalize(string $value): string
    {
        return PlatformIdGenerator::normalize($value);
    }

    public static function unique(string $candidate, array $existingIds): string
    {
        return PlatformIdGenerator::unique($candidate, $existingIds);
    }

    public static function isValid(string $id): bool
    {
        return PlatformIdGenerator::isValid($id);
    }

    public static function fromContext(
        string $type,
        string $table = '',
        string $field = '',
        string $label = ''
    ): string {
        $base = match ($type) {
            'field' => trim($table . '_' . $field, '_'),
            'table-grid' => trim($table . '_grid', '_'),
            'button', 'custom-button' => self::normalize($label ?: 'button') . '_btn',
            'chart' => self::normalize($label ?: 'chart') . '_chart',
            'section' => self::normalize($label ?: 'section') . '_section',
            'row' => self::normalize($label ?: 'row') . '_row',
            'stat-card' => self::normalize($label ?: 'stat') . '_card',
            'kpi-trend-card' => self::normalize($label ?: 'kpi') . '_kpi',
            default => self::normalize($label ?: $type),
        };

        return self::normalize($base);
    }
}
