<?php

declare(strict_types=1);

require_once __DIR__ . '/api_service.php';

function admin_resolve_download_contract(string $token, int $id = 0): ?array
{
    $token = trim($token);
    $contract = null;

    if ($token !== '') {
        $contract = api_find_contract($token);
    }

    if ($contract === null && $id > 0) {
        $contract = local_store_find_by_id($id);
        if ($contract === null && api_auth_enabled()) {
            $contract = api_find_partner_contract_by_token((string)$id);
        }
    }

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

    return api_merge_contract_with_local($contract);
}
