#!/usr/bin/env php

<?php

function dashesToCamelCase($string, $capitalizeFirstCharacter = false)
{
    $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
    if (!$capitalizeFirstCharacter) {
        $str[0] = strtolower($str[0]);
    }

    return $str;
}

$iconSets = [
    'heroicons' => [
        'path'      => "src/view/frontend/web/svg/heroicons/outline",
        'namespace' => 'Hyva\Theme\ViewModel',
        'interface' => 'Heroicons',
    ],
    'lucide'    => [
        'path'      => "src/view/base/web/svg/lucide",
        'namespace' => 'Hyva\Theme\ViewModel',
        'interface' => 'LucideIcons',
    ],
];

foreach ($iconSets as $iconSetKey => $iconSet) {
    $dir        = dirname(__DIR__, 1) . "/" . $iconSet['path'];
    $icons      = array_diff(scandir($dir), array('..', '.'));
    $outputPath = dirname(__DIR__, 1) . "/src/ViewModel/" . $iconSet['interface'] . ".php";

    // Clean up file first
    file_put_contents($outputPath, "");

    // Set Head
    file_put_contents(
      $outputPath,
      "<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

namespace " . $iconSet['namespace'] . ";

// phpcs:disable Magento2.NamingConvention.InterfaceName.WrongInterfaceName
// phpcs:disable Generic.Files.LineLength.TooLong

/**
 * These method signatures were generated with bin/generate-icon-signatures
 *
");

    // Create interface code
    foreach ($icons as &$icon) {
        $icon = str_replace('.svg', '', $icon);

        $string = " * @method string "
            . dashesToCamelCase($icon)
            . "Html(string \$classnames = '', ?int \$width = 24, ?int \$height = 24, array \$attributes = [])";

        file_put_contents($outputPath, $string . "\n", FILE_APPEND);
    }

    // Set closing part of interface
    if ($iconSetKey === "lucide") {
        file_put_contents($outputPath, " */\nclass " . $iconSet['interface'] . " extends SvgIcons\n{\n}\n", FILE_APPEND);
    } else {
        file_put_contents($outputPath, " */\ninterface " . $iconSet['interface'] . "\n{\n}\n", FILE_APPEND);
    }
}
