import { __awaiter, __generator } from "tslib";
import { buildIPFSClient, retrieveValueFromIPFS, storeValueInIPFS } from "../../utils/IPFSUtils";
import { jsonReviverWithBigNumbers, jsonReplacerWithBigNumbers, formattedLog } from "../../utils";
/**
 * A client for interacting with the IPFS. This is a wrapper around the IPFS API.
 * This client also is a part of the caching mechanism interface.
 * @see https://docs.ipfs.io/reference/http/api/
 */
var PinataIPFSClient = /** @class */ (function () {
    function PinataIPFSClient(projectId, projectSecret, publicGatewayURL, logger) {
        this.client = buildIPFSClient(projectId, projectSecret);
        this.publicGatewayURL = publicGatewayURL;
        this.logger = logger;
    }
    /**
     * Calls to a public IPFS gateway to retrieve a value.
     * @param key The key to retrieve.
     * @param _structValidator An optional struct validator to validate the retrieved value. If the value does not match the struct, null is returned.
     * @returns The value if it exists, otherwise null.
     */
    PinataIPFSClient.prototype.get = function (key, _structValidator) {
        return __awaiter(this, void 0, void 0, function () {
            var arrivedResult, revivedResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!key) {
                            return [2 /*return*/, null];
                        }
                        formattedLog(this.logger, {
                            level: "debug",
                            message: "Retrieving value from IPFS with key ".concat(key),
                            at: {
                                location: "IPFSClient",
                                function: "get",
                            },
                        });
                        return [4 /*yield*/, retrieveValueFromIPFS(key, this.publicGatewayURL)];
                    case 1:
                        arrivedResult = _a.sent();
                        if (!arrivedResult) {
                            return [2 /*return*/, null];
                        }
                        revivedResult = JSON.parse(arrivedResult, jsonReviverWithBigNumbers);
                        if (_structValidator && !_structValidator.is(revivedResult)) {
                            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 /*return*/, null];
                        }
                        return [2 /*return*/, revivedResult];
                }
            });
        });
    };
    /**
     * Stores a value in the IPFS and returns the CID of the value stored.
     * @param key An optional key to store the value with. This is purely in the metadata of the CID.
     * @param value The value to store.
     * @returns The CID of the value stored.
     */
    PinataIPFSClient.prototype.set = function (key, value) {
        formattedLog(this.logger, {
            level: "debug",
            message: "Setting value from IPFS with key ".concat(key),
            at: {
                location: "IPFSClient",
                function: "setWithReturnID",
            },
        });
        return storeValueInIPFS(key, JSON.stringify(value, 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;
}());
export { PinataIPFSClient };
//# sourceMappingURL=PinataIPFSClient.js.map