import { kinds } from "nostr-tools";
import { canHaveEncryptedContent, EncryptedContentSymbol, getEncryptedContent, getEncryptedContentEncryptionMethods, hasEncryptedContent, isEncryptedContentLocked, lockEncryptedContent, setEncryptedContentCache, setEncryptedContentEncryptionMethod, } from "./encrypted-content.js";
// reexport from encrypted-content
export const HiddenContentSymbol = EncryptedContentSymbol;
export const getHiddenContentEncryptionMethods = getEncryptedContentEncryptionMethods;
/** Various event kinds that can have hidden content */
export const HiddenContentKinds = new Set([setEncryptedContentEncryptionMethod(kinds.DraftLong, "nip04")]);
/** Sets the encryption method for hidden content on a kind */
export function setHiddenContentEncryptionMethod(kind, method) {
    HiddenContentKinds.add(setEncryptedContentEncryptionMethod(kind, method));
    return kind;
}
/** Checks if an event can have hidden content */
export function canHaveHiddenContent(kind) {
    return canHaveEncryptedContent(kind) && HiddenContentKinds.has(kind);
}
/** Checks if an event has hidden content */
export function hasHiddenContent(event) {
    return canHaveHiddenContent(event.kind) && hasEncryptedContent(event);
}
/** Checks if the hidden content is locked */
export function isHiddenContentLocked(event) {
    return isEncryptedContentLocked(event);
}
/** Returns the hidden content for an event if they are unlocked */
export function getHiddenContent(event) {
    if (!canHaveHiddenContent(event.kind) || isHiddenContentLocked(event))
        return undefined;
    return getEncryptedContent(event);
}
/**
 * Unlocks the hidden content in the event
 * @param event The event with content to decrypt
 * @param signer A signer to use to decrypt the content
 * @throws
 */
export async function unlockHiddenContent(event, signer, override) {
    if (!canHaveHiddenContent(event.kind))
        throw new Error("Event kind does not support hidden content");
    const encryption = getEncryptedContentEncryptionMethods(event.kind, signer, override);
    const plaintext = await encryption.decrypt(event.pubkey, event.content);
    setHiddenContentCache(event, plaintext);
    return plaintext;
}
/**
 * Sets the hidden content on an event and updates it if its part of an event store
 * @throws
 */
export function setHiddenContentCache(event, plaintext) {
    if (!canHaveHiddenContent(event.kind))
        throw new Error("Event kind does not support hidden content");
    // Set the encrypted content
    setEncryptedContentCache(event, plaintext);
}
/** Removes the unencrypted hidden content on an event */
export function lockHiddenContent(event) {
    lockEncryptedContent(event);
}
