<?php

namespace WHMCS\Module\Addon\Proxycheap;

use WHMCS\Database\Capsule as DB;

class APIHelper
{

    public static function getApiCredentials()
    {
        $configuration = collect(DB::table('tbladdonmodules')->where('module', 'proxycheap')->whereIn('setting', ['apikey', 'apisecret', 'customhostname'])->get(['setting', 'value']))->keyBy('setting');

        if ($configuration) {

            return ['apikey' => $configuration['apikey']->value, 'apisecret' => $configuration['apisecret']->value, 'residentialDomain' => $configuration['customhostname']->value];
        }

        return false;
    }

    public static function filterParamsByProxyType(array $params = [])
    {
        $input = $params;
        if ($input['serviceType']) {


            switch ($input['serviceType']) {
                case 'PROXY_RESIDENTIAL_STATIC':
                case 'PROXY_DATACENTER':
                case 'PROXY_MOBILE':
                    unset($input['bandwidth']);
                    break;
                case 'PROXY_RESIDENTIAL':
                    unset($input['quantity']);
                    break;
            }
        }
        return $input;

    }

    public static function trimCF($cfs)
    {
        $cf = [];
        foreach ($cfs as $k => $cff) {
            $cf[trim($k)] = trim($cff);
        }

        return $cf;
    }
}
