<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Runtime;

use APD\Support\SvgSanitizer;

defined('ABSPATH') || exit;

class RuntimeShapeMapper
{
    public function normalizeItems(array $shapes) : array
    {
        $items = [];
        foreach ($shapes as $shape) {
            if (! is_array($shape) || (isset($shape['enabled']) && ! $shape['enabled'])) {
                continue;
            }

            $id = sanitize_key((string) ($shape['id'] ?? ''));
            $label = sanitize_text_field((string) ($shape['label'] ?? ''));
            $svgText = SvgSanitizer::sanitize((string) ($shape['svgText'] ?? ''));
            if ($id === '' || $label === '' || $svgText === '') {
                continue;
            }

            $items[] = array_replace($shape, [
                'id' => $id,
                'name' => sanitize_text_field((string) ($shape['name'] ?? $label)),
                'label' => $label,
                'svgText' => $svgText,
                'itemType' => 'shape',
            ]);
        }

        return $items;
    }
}
