"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeFeedIdentifier = void 0;
const types_1 = require("../types");
const hash_1 = require("../utils/hash");
const hex_1 = require("../utils/hex");
const uint64_1 = require("../utils/uint64");
function isEpoch(epoch) {
    return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
}
function hashFeedIdentifier(topic, index) {
    return (0, hash_1.keccak256Hash)((0, hex_1.hexToBytes)(topic), index);
}
function makeSequentialFeedIdentifier(topic, index) {
    const indexBytes = (0, uint64_1.writeUint64BigEndian)(index);
    return hashFeedIdentifier(topic, indexBytes);
}
function makeFeedIndexBytes(s) {
    const hex = (0, hex_1.makeHexString)(s, types_1.FEED_INDEX_HEX_LENGTH);
    return (0, hex_1.hexToBytes)(hex);
}
function makeFeedIdentifier(topic, index) {
    if (typeof index === 'number') {
        return makeSequentialFeedIdentifier(topic, index);
    }
    else if (typeof index === 'string') {
        const indexBytes = makeFeedIndexBytes(index);
        return hashFeedIdentifier(topic, indexBytes);
    }
    else if (isEpoch(index)) {
        throw new TypeError('epoch is not yet implemented');
    }
    return hashFeedIdentifier(topic, index);
}
exports.makeFeedIdentifier = makeFeedIdentifier;
