"use strict";
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 effect_1 = require("effect");
const rxjs_1 = require("rxjs");
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, effect_1.Option.fromNullable, effect_1.Option.map(({ protocol, schemaOrHash }) => fetchProtocolSchema[protocol](schemaOrHash)));
//# sourceMappingURL=getStreamSchema.js.map