<?php

namespace PHPNative\Tailwind\Parser;

use PHPNative\Tailwind\Style\DirectionEnum;
use PHPNative\Tailwind\Style\FlexTypeEnum;

class Flex implements Parser
{

    public static function parse(string $style): ?\PHPNative\Tailwind\Style\Style
    {
        preg_match_all('/flex-none/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Flex(type:FlexTypeEnum::none);
        }
        preg_match_all('/flex-grow/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Flex(type:FlexTypeEnum::grow);
        }
        preg_match_all('/flex-1/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Flex(type:FlexTypeEnum::one);
        }
        preg_match_all('/flex-auto/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Flex(type:FlexTypeEnum::auto);
        }
        preg_match_all('/flex-col/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Flex(DirectionEnum::column);
        }

        preg_match_all('/(?!flex-col)(flex-row|flex)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Flex(DirectionEnum::row);
        }

        return null;

    }

    public static function merge(\PHPNative\Tailwind\Style\Flex $class, \PHPNative\Tailwind\Style\Flex $style)
    {
        if($style->type != FlexTypeEnum::none) {
            $class->type = $style->type;
        }
        if($style->direction != DirectionEnum::row) {
            $class->direction = $style->direction;
        }
    }
}