<?php

namespace App\Data\Pagarme;

abstract readonly class PagarmeResponseData
{
    abstract public static function fromArray(array $payload): static;

    abstract public function toArray(): array;

    //

    protected static function arrGet(array $payload, int|string $key, mixed $default = null): mixed
    {
        return data_get($payload, $key, $default);
    }

    protected static function arrInt(array $payload, string $key): ?int
    {
        $value = data_get($payload, $key);

        if ($value === null) {
            return null;
        }

        return (int) $value;
    }

    protected static function arrString(array $payload, string $key): ?string
    {
        $value = data_get($payload, $key);

        return $value !== null && $value !== '' ? (string) $value : null;
    }

    protected static function arrBool(array $payload, string $key): ?bool
    {
        $value = data_get($payload, $key);

        return $value !== null && $value !== '' ? (bool) $value : null;
    }

    protected static function arrArray(array $payload, string $key): array
    {
        $value = data_get($payload, $key, []);

        return is_array($value) ? $value : [];
    }

    protected static function arrMap(array $payload, string $key, callable $fn): array
    {
        return array_map($fn, static::arrArray($payload, $key));
    }
}
