<?php
function loadPrintifyConfigs($value) {
    $configs = [];
    if(isset($value['accounts']) && count($value['accounts'])) {
        $accounts = $value['accounts'];
        foreach ($accounts as $id => $acc) {

            $configs[] = [
                'name' => $acc['name'],
                'shop_id' => $id,
                'api_token' => $acc['api_token'],
                'default' => $acc['default']
            ];

        }
    }

    return $configs;
}

function loadGelatoConfigs($value) {
    $config = [];
    if(isset($value['accounts']) && count($value['accounts'])) {
        $accounts = $value['accounts'];
        foreach ($accounts as $acc) {
            if(isset($acc['default']) && $acc['default'] == true) {
                $config = array_merge($config, $acc);
            }
        }
    }
    return $config;
}

function loadMerchizeConfigs($value) {
    $config = [];
    if(isset($value['accounts']) && count($value['accounts'])) {
        $accounts = $value['accounts'];
        foreach ($accounts as $acc) {
            if(isset($acc['default']) && $acc['default'] == true) {
                $config = array_merge($config, $acc);
            }
        }
    }
    return $config;
}

function loadPrintLogisticConfigs($value)
{
    $config = [];
    if(isset($value['accounts']) && count($value['accounts'])) {
        $accounts = $value['accounts'];
        foreach ($accounts as $acc) {
            if(isset($acc['default']) && $acc['default'] == true) {
                $config = array_merge($config, $acc);
            }
        }
    }
    return $config;
}

function loadProLuxuryConfigs($value)
{
    $config = [];
    if(isset($value['accounts']) && count($value['accounts'])) {
        $accounts = $value['accounts'];
        foreach ($accounts as $acc) {
            if(isset($acc['default']) && $acc['default'] == true) {
                $config = array_merge($config, $acc);
            }
        }
    }
    return $config;
}

function loadTeezilyConfigs($value)
{
    $config = [];
    if(isset($value['accounts']) && count($value['accounts'])) {
        $accounts = $value['accounts'];
        foreach ($accounts as $acc) {
            if(isset($acc['default']) && $acc['default'] == true) {
                $config = array_merge($config, $acc);
            }
        }
    }
    return $config;
}

if (!function_exists('getCustomerName')) {
    /**
     * @param $fullName
     * @return array
     */
    function getCustomerName($fullName)
    {
        $partsName = explode(' ', $fullName);
        $lengthName = count($partsName);

        $lastName = array_pop($partsName);
        $firstName = implode(' ', $partsName);

        if ($lengthName < 2) {
            $firstName = $lastName;
        }

        $lastName = ucwords($lastName);
        $firstName = ucwords($firstName);

        return [
            'last_name' => $lastName,
            'first_name' => $firstName
        ];
    }
}

if (!function_exists('isset_not_empty')) {
    function isset_not_empty(&$variable, &...$variables): bool
    {
        array_push($variables, $variable);
        foreach ($variables as $_var) {
            if (!(isset($_var) && !empty($_var))) return false;
        }
        return true;
    }
}
