"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PinataIPFSClient = void 0;
var tslib_1 = require("tslib");
var IPFSUtils_1 = require("../../utils/IPFSUtils");
var utils_1 = require("../../utils");
var PinataIPFSClient = (function () {
    function PinataIPFSClient(projectId, projectSecret, publicGatewayURL, logger) {
        this.client = (0, IPFSUtils_1.buildIPFSClient)(projectId, projectSecret);
        this.publicGatewayURL = publicGatewayURL;
        this.logger = logger;
    }
    PinataIPFSClient.prototype.get = function (key, _structValidator) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var arrivedResult, revivedResult;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!key) {
                            return [2, null];
                        }
                        (0, utils_1.formattedLog)(this.logger, {
                            level: "debug",
                            message: "Retrieving value from IPFS with key ".concat(key),
                            at: {
                                location: "IPFSClient",
                                function: "get",
                            },
                        });
                        return [4, (0, IPFSUtils_1.retrieveValueFromIPFS)(key, this.publicGatewayURL)];
                    case 1:
                        arrivedResult = _a.sent();
                        if (!arrivedResult) {
                            return [2, null];
                        }
                        revivedResult = JSON.parse(arrivedResult, utils_1.jsonReviverWithBigNumbers);
                        if (_structValidator && !_structValidator.is(revivedResult)) {
                            (0, utils_1.formattedLog)(this.logger, {
                                level: "warn",
                                message: "Retrieved value from IPFS with key ".concat(key, " does not match the expected type"),
                                at: {
                                    location: "IPFSClient",
                                    function: "get",
                                },
                            });
                            return [2, null];
                        }
                        return [2, revivedResult];
                }
            });
        });
    };
    PinataIPFSClient.prototype.set = function (key, value) {
        (0, utils_1.formattedLog)(this.logger, {
            level: "debug",
            message: "Setting value from IPFS with key ".concat(key),
            at: {
                location: "IPFSClient",
                function: "setWithReturnID",
            },
        });
        return (0, IPFSUtils_1.storeValueInIPFS)(key, JSON.stringify(value, utils_1.jsonReplacerWithBigNumbers), this.client);
    };
    PinataIPFSClient.prototype.sub = function (_topic, _callback) {
        return Promise.resolve(1);
    };
    PinataIPFSClient.prototype.pub = function (_topic, _message) {
        return Promise.resolve(1);
    };
    return PinataIPFSClient;
}());
exports.PinataIPFSClient = PinataIPFSClient;
//# sourceMappingURL=PinataIPFSClient.js.map