<?php

declare(strict_types=1);

namespace PHPNative\Tailwind\Parser;

use PHPNative\Tailwind\Style\Unit;

class Height implements Parser
{
    public static function parse(string $style): ?\PHPNative\Tailwind\Style\Height
    {
        $value = -1;
        $unit = Unit::Pixel;
        $found = false;

        if (preg_match('/(min-)?h-\[(\d+)\]/', $style, $output_array)) {
            $value = (int)$output_array[2];
            $found = true;
        }
        preg_match_all('/h-(\d*)\/(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $value1 = (int)$output_array[1][0];
            $value2 = (int)$output_array[2][0];
            $unit = Unit::Percent;
            $value = 100/$value2*$value1;
            $found = true;
        }

        preg_match_all('/h-(\d*)/', $style, $output_array);
        if (!$found && count($output_array[0]) > 0) {
            // Tailwind uses a 4px scale: h-1 = 4px, h-10 = 40px, etc.
            $value = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/(h-full|h-screen)/', $style, $output_array);
        if (!$found && count($output_array[0]) > 0) {
            $value = 100;
            $unit = Unit::Percent;
        }



        if($value != -1) {
            return new \PHPNative\Tailwind\Style\Height($unit, $value);
        }

        return null;
    }

    public static function merge(\PHPNative\Tailwind\Style\Padding $class, \PHPNative\Tailwind\Style\Padding $style)
    {
        if($style->left != null) {
            $class->left = $style->left;
        }
        if($style->right != null) {
            $class->right = $style->right;
        }
        if($style->top != null) {
            $class->top = $style->top;
        }
        if($style->bottom != null) {
            $class->bottom = $style->bottom;
        }
    }
}
