import * as dagPB from "@ipld/dag-pb";
import * as ipfs from "../../ipfs/index.js";
import { DAG_NODE_DATA } from "../../ipfs/constants.js";
import { decodeCID } from "../../common/index.js";
import * as check from "../types/check.js";
import * as link from "../link.js";
import * as typeCheck from "../../common/type-checks.js";
export const getFile = async (cid) => {
    return ipfs.catBuf(cid);
};
export const getEncryptedFile = async (cid, key) => {
    return ipfs.encoded.catAndDecode(cid, key);
};
export const putFile = async (content) => {
    return ipfs.add(content);
};
export const putEncryptedFile = async (content, key) => {
    return ipfs.encoded.add(content, key);
};
export const getSimpleLinks = async (cid) => {
    const dagNode = await ipfs.dagGet(cid);
    return link.arrToMap(dagNode.Links.map(link.fromDAGLink));
};
export const getFileSystemLinks = async (cid) => {
    const topNode = await ipfs.dagGet(cid);
    const links = await Promise.all(topNode.Links.map(async (l) => {
        const innerNode = await ipfs.dagGet(l.Hash);
        const innerLinks = link.arrToMap(innerNode.Links.map(link.fromDAGLink));
        const isSoftLink = !!innerLinks["softLink"];
        if (isSoftLink) {
            const a = await ipfs.catBuf(decodeCID(innerLinks["softLink"].cid));
            const b = new TextDecoder().decode(a);
            return JSON.parse(b);
        }
        const f = await ipfs.encoded.catAndDecode(decodeCID(innerLinks["metadata"].cid), null);
        return {
            ...link.fromDAGLink(l),
            isFile: typeCheck.hasProp(f, "isFile") ? f.isFile : false
        };
    }));
    return link.arrToMap(links);
};
export const putLinks = async (links) => {
    const dagLinks = Object.values(links)
        .filter(l => l !== undefined)
        .map(async (l) => {
        if (check.isSoftLink(l)) {
            const softLink = await ipfs.add(JSON.stringify(l));
            const dagNode = await ipfs.dagPut(dagPB.createNode(DAG_NODE_DATA, [
                dagPB.createLink("softLink", softLink.size, softLink.cid)
            ]));
            return dagPB.createLink(l.name, dagNode.size, dagNode.cid);
        }
        else if (l.Hash) {
            return l;
        }
        else {
            return link.toDAGLink(l);
        }
    });
    return ipfs.dagPutLinks(await Promise.all(dagLinks));
};
//# sourceMappingURL=basic.js.map