"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSchemaFromMetadata = exports.defaultAjv = void 0;
const ajv_1 = __importDefault(require("ajv"));
const ajv_formats_1 = __importDefault(require("ajv-formats"));
const rxjs_1 = require("rxjs");
const Option = __importStar(require("effect/Option"));
exports.defaultAjv = new ajv_1.default({
    useDefaults: true,
    discriminator: true,
});
(0, ajv_formats_1.default)(exports.defaultAjv);
exports.defaultAjv.addFormat('ethereum-address', /^0x[a-zA-Z0-9]{40}$/);
const getSchemaConfigFromMetadata = (metadata) => {
    // @ts-expect-error metadata isn't stated by default by streamr
    return metadata.logstore?.schema;
};
// todo: move to logstore client
const fetchProtocolSchema = {
    // todo: is some form of sanitizing needed here?
    IPFS: async (hash) => {
        const res = await fetch(`https://ipfs.io/ipfs/${hash}`);
        const schema = await res.json();
        await exports.defaultAjv.validateSchema(schema, true);
        return schema;
    },
    ARWEAVE: async (hash) => {
        const res = await fetch(`https://arweave.net/${hash}`);
        const schema = await res.json();
        await exports.defaultAjv.validateSchema(schema, true);
        return schema;
    },
    RAW: async (schema) => {
        await exports.defaultAjv.validateSchema(schema, true);
        return schema;
    },
};
exports.getSchemaFromMetadata = (0, rxjs_1.pipe)(getSchemaConfigFromMetadata, Option.fromNullable, Option.map(({ protocol, schemaOrHash }) => fetchProtocolSchema[protocol](schemaOrHash)));
//# sourceMappingURL=getStreamSchema.js.map