'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../../../_virtual/_tslib.cjs');
var utils = require('@dynamic-labs/utils');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react');
require('react/jsx-runtime');
require('../../context/ViewContext/ViewContext.cjs');
var logger = require('../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../utils/constants/colors.cjs');
require('../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../shared/consts/index.cjs');
var api = require('./api.cjs');

const RATE_LIMIT_MESSAGE = 'Too many requests. Please try again later.';
const NETWORK_ERROR_MESSAGE = 'Request failed. You may be sending too many requests — please wait a moment and try again.';
// expo/fetch returns response objects that aren't the same class as global Response,
// so instanceof checks fail on React Native / Expo SDK 56+. Duck-type instead.
const isResponseLike = (e) => typeof (e === null || e === void 0 ? void 0 : e.json) === 'function' &&
    typeof (e === null || e === void 0 ? void 0 : e.status) === 'number';
const logResponseError = (e_1, message_1, ...args_1) => _tslib.__awaiter(void 0, [e_1, message_1, ...args_1], void 0, function* (e, message, logLevel = 'error') {
    var _a, _b, _c, _d, _e, _f, _g;
    if (isResponseLike(e)) {
        const requestId = (_b = (_a = e.headers) === null || _a === void 0 ? void 0 : _a.get('x-dyn-request-id')) !== null && _b !== void 0 ? _b : undefined;
        const cfRay = (_d = (_c = e.headers) === null || _c === void 0 ? void 0 : _c.get('cf-ray')) !== null && _d !== void 0 ? _d : undefined;
        let data;
        try {
            data = yield e.json();
        }
        catch (_h) {
            data = {};
        }
        // Normalize 429 responses regardless of body content
        if (e.status === 429) {
            data.code = utils.RATE_LIMIT_ERROR_CODE;
            data.error = (_e = data.error) !== null && _e !== void 0 ? _e : RATE_LIMIT_MESSAGE;
        }
        data.code = (_f = data.code) !== null && _f !== void 0 ? _f : utils.UNKNOWN_ERROR_CODE;
        data.error = (_g = data.error) !== null && _g !== void 0 ? _g : `Request failed with status ${e.status}`;
        logger.logger[logLevel](message, {
            cfRay,
            error: data.error,
            requestId,
            status: e.status,
        });
        return data;
    }
    if (e instanceof Error) {
        logger.logger[logLevel](message, e);
    }
    return {
        code: utils.UNKNOWN_ERROR_CODE,
        error: NETWORK_ERROR_MESSAGE,
    };
});
const NONCE_FETCH_FAILED_EVENT = 'nonce.fetch_failed';
const getNonce = (environmentId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    var _j, _k, _l, _m;
    try {
        const res = yield api.sdkApi().getNonce({
            environmentId,
        });
        if (!res.nonce) {
            logger.logger.instrument(NONCE_FETCH_FAILED_EVENT, {
                environmentId,
                key: NONCE_FETCH_FAILED_EVENT,
                reason: 'empty_response',
                time: 0,
            });
        }
        return res.nonce;
    }
    catch (e) {
        const errorMeta = isResponseLike(e)
            ? {
                cfRay: (_k = (_j = e.headers) === null || _j === void 0 ? void 0 : _j.get('cf-ray')) !== null && _k !== void 0 ? _k : undefined,
                requestId: (_m = (_l = e.headers) === null || _l === void 0 ? void 0 : _l.get('x-dyn-request-id')) !== null && _m !== void 0 ? _m : undefined,
                status: e.status,
            }
            : { error: e instanceof Error ? e.message : String(e) };
        logger.logger.instrument(NONCE_FETCH_FAILED_EVENT, Object.assign({ environmentId, key: NONCE_FETCH_FAILED_EVENT, reason: 'request_failed', time: 0 }, errorMeta));
        return undefined;
    }
});

exports.getNonce = getNonce;
exports.isResponseLike = isResponseLike;
exports.logResponseError = logResponseError;
