import { SymmAlg } from "keystore-idb/types.js";
import * as cbor from "@ipld/dag-cbor";
import { isBlob, isDefined, isJust, isString } from "../common/type-checks.js";
import * as blob from "../common/blob.js";
import * as crypto from "../crypto/index.js";
import * as basic from "./basic.js";
export const add = async (content, key) => {
    // can't cbor encode blobs ie file streams
    const normalized = isBlob(content) ? await blob.toUint8Array(content) : content;
    const encoded = cbor.encode(normalized);
    if (!isJust(key)) {
        return basic.add(encoded);
    }
    const alg = SymmAlg.AES_GCM;
    const cip = await crypto.aes.encrypt(encoded, key, alg);
    const toAdd = cbor.encode({ alg, cip });
    return basic.add(toAdd);
};
export const catAndDecode = async (cid, key) => {
    const buf = await basic.catBuf(cid);
    if (!isJust(key)) {
        return cbor.decode(buf);
    }
    const withAlgorithm = cbor.decode(buf);
    if (!isSymmAlg(withAlgorithm.alg) || !isDefined(withAlgorithm.cip)) {
        throw new Error(`Unexpected private block. Expected "alg" and "cip" field.`);
    }
    const alg = withAlgorithm.alg;
    const toDecode = await crypto.aes.decrypt(withAlgorithm.cip, key, alg);
    return cbor.decode(toDecode);
};
function isSymmAlg(alg) {
    return isString(alg) && Object.values(SymmAlg).includes(alg);
}
//# sourceMappingURL=encoded.js.map