<?php

// Path to the input JSON file
$inputFile = 'font-awesome-v6.1.7-solid-svgs.json';

// Step 1: Load the JSON data
$jsonData = file_get_contents($inputFile);

// Check if the file was read successfully
if ($jsonData === false) {
    die("Error reading the JSON file.");
}

// Step 2: Decode the JSON data into a PHP array
$data = json_decode($jsonData, true);

// Check if JSON decoding was successful
if ($data === null) {
    die("Error decoding the JSON data.");
}

// Step 3: Modify the structure by extracting the 'd' value and formatting the 'paths' tag
foreach ($data as $index => $iconSet) {
    if (isset($iconSet['svgs']) && is_array($iconSet['svgs'])) {
        foreach ($iconSet['svgs'] as $svgIndex => $svg) {
            // Check if 'path' exists
            if (isset($svg['path'])) {
                // Extract the 'd' attribute value from the nested path string
                preg_match('/d=[\'"]([^\'"]+)[\'"]/i', $svg['path'], $matches);

                // If we found the 'd' value, format the paths correctly
                if (isset($matches[1])) {
                    $dValue = $matches[1]; // Get the actual 'd' value from the path

                    // Escape the 'd' value for JSON format (escape double quotes inside the string)
                    $escapedDValue = str_replace('"', '\\"', $dValue);

                    // Now, add the proper <path> tag to the 'paths' field
                    $svg['paths'] = "<path d=\"{$escapedDValue}\"/>";
                }

                unset($svg['path']); // Optionally remove the original 'path' key
            }
            // Save the modified svg back to the array
            $data[$index]['svgs'][$svgIndex] = $svg;
        }
    }
}

// Optional: Save the modified JSON to a new file
$outputFile = 'solid-fixed-paths.json';
file_put_contents($outputFile, json_encode($data, JSON_PRETTY_PRINT));

// Output the success message
echo "Successfully modified and saved the JSON file with 'path' converted to 'paths'.\n";

?>
