<?php

namespace App\Nova\Fields;

use Closure;

class FieldHelpers
{
    /**
     * Format products stock for better visability
     */
    public static function formatQuantity(null|int|float|string $count = null): Closure
    {
        return function ($resource) use ($count) {
            $stock = $count ?: $resource->stock;

            return sprintf('
                <div class="flex items-center">
                    <span class="text-xs px-1 inline-flex leading-5 font-semibold rounded-full %s" style="padding-right: 7px;padding-left: 7px;">
                        %s
                    </span>
                </div>
                ',
                $stock < 10 ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-darker',
                $stock
            );
        };
    }

    /**
     * Show as link
     *
     * @param  string  $title
     */
    public static function asLink(string $link, int|string|null $title): Closure
    {
        return fn () => sprintf('<a href="%s" target="_blank" class="link-default"> %s </a>', $link, $title);
    }

    /**
     * To turkmen date d.m.Y
     */
    public static function tmDate(): Closure
    {
        return fn ($value) => $value?->format('d.m.Y');
    }

    /**
     * Has permission for canSee or canRun methods
     */
    public static function hasPermission(string $permissionName = 'systemUsers'): Closure
    {
        return fn ($request) => $request->user()->can('isAdmin');
    }
}
