<?php

declare(strict_types=1);

namespace Psl\Type\Internal;

use Override;
use Psl\Type;
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function array_is_list;
use function array_values;
use function is_array;

/**
 * @extends Type\Type<list<mixed>>
 *
 * @internal
 */
final readonly class MixedVecType extends Type\Type
{
    /**
     * @psalm-assert-if-true list<Tv> $value
     */
    #[Override]
    public function matches(mixed $value): bool
    {
        return is_array($value) && array_is_list($value);
    }

    /**
     * @throws CoercionException
     *
     * @return list<mixed>
     */
    #[Override]
    public function coerce(mixed $value): iterable
    {
        if (!is_iterable($value)) {
            throw CoercionException::withValue($value, $this->toString());
        }

        if (is_array($value)) {
            if (!array_is_list($value)) {
                return array_values($value);
            }

            return $value;
        }

        /**
         * @var list<mixed> $entries
         */
        $result = [];

        /**
         * @var mixed $v
         */
        foreach ($value as $v) {
            $result[] = $v;
        }

        return $result;
    }

    /**
     * @throws AssertException
     *
     * @return list<mixed>
     *
     * @psalm-assert list<mixed> $value
     */
    #[Override]
    public function assert(mixed $value): array
    {
        if (!is_array($value) || !array_is_list($value)) {
            throw AssertException::withValue($value, $this->toString());
        }

        return $value;
    }

    #[Override]
    public function toString(): string
    {
        return 'vec<mixed>';
    }
}
