<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class FbaInboundApi extends BaseApi
{
    /**
     * Operation Get Item Eligibility Preview
     *
     * @param string|array $params['marketplaceIds'] he identifier for the marketplace in which you want to determine eligibility. Required only when program=INBOUND.
     * @param string $params['asin'] The ASIN of the item for which you want an eligibility preview. (required)
     * @param string $params['program'] The program that you want to check eligibility against. (required)
     *
     * @return json\
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     */
    public function getItemEligibilityPreview($params)
    {
        if (!array_key_exists('asin', $params) ||  null === $params['asin']) {
            throw new \InvalidArgumentException('Missing the required parameter $asin when calling getOrderItemsBuyerInfo');
        }

        if (!array_key_exists('program', $params) ||  null === $params['program']) {
            throw new \InvalidArgumentException('Missing the required parameter $program when calling getOrderItemsBuyerInfo');
        }
        if (!array_key_exists('marketplaceIds', $params) ||  null === $params['marketplaceIds'] &&  strtoupper($params['program']) == "INBOUND") {
            throw new \InvalidArgumentException('Missing the required parameter $marketplaceIds when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/fba/inbound/v1/eligibility/itemPreview';
        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        // query params
        if (array_key_exists('marketplaceIds', $params) && $params['marketplaceIds'] !== null) {
            $query_params['marketplaceIds'] = is_array($params['marketplaceIds']) ? implode(',', $params['marketplaceIds']) :  $params['marketplace_ids'];
        }

        if (array_key_exists('asin', $params) && $params['asin'] !== null) {
            $query_params['asin'] = $params['asin'];
        }
        if (array_key_exists('program', $params) && $params['program'] !== null) {
            $query_params['program'] = $params['program'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }
}
