import { CID } from "multiformats/cid";
import * as dagPB from "@ipld/dag-pb";
import * as uint8arrays from "uint8arrays";
import { DAG_NODE_DATA } from "./constants.js";
import { get as getIpfs } from "./config.js";
import { decodeCID, isObject, isString } from "../common/index.js";
import { setup } from "../setup/internal.js";
import * as util from "./util.js";
export const add = async (content) => {
    const ipfs = await getIpfs();
    const result = await ipfs.add(content, { cidVersion: 1, pin: setup.shouldPin });
    return {
        cid: decodeCID(result.cid),
        size: result.size,
        isFile: true
    };
};
export const catRaw = async (cid) => {
    const newCID = typeCheckCID(cid);
    const ipfs = await getIpfs();
    const chunks = [];
    await attemptPin(newCID);
    for await (const chunk of ipfs.cat(newCID)) {
        chunks.push(chunk);
    }
    return chunks;
};
export const catBuf = async (cid) => {
    const newCID = typeCheckCID(cid);
    const raw = await catRaw(newCID);
    return uint8arrays.concat(raw);
};
export const cat = async (cid) => {
    const newCID = typeCheckCID(cid);
    const buf = await catBuf(newCID);
    return buf.toString();
};
export const ls = async (cid) => {
    const newCID = typeCheckCID(cid);
    const ipfs = await getIpfs();
    const links = [];
    for await (const link of ipfs.ls(newCID)) {
        links.push({ ...link, cid: decodeCID(link.cid) });
    }
    return links;
};
export const dagGet = async (cid) => {
    const newCID = typeCheckCID(cid);
    const ipfs = await getIpfs();
    await attemptPin(newCID);
    const raw = await ipfs.dag.get(newCID);
    const node = util.rawToDAGNode(raw);
    return node;
};
export const dagPut = async (node) => {
    const ipfs = await getIpfs();
    const newNode = dagPB.createNode(node.Data || new Uint8Array(), node.Links.map(link => dagPB.createLink(link.Name || "", link.Tsize || 0, link.Hash)));
    // Using this format because Gateway doesn't like `dag-cbor` nodes.
    // I think this is because UnixFS requires `dag-pb` & the gateway requires UnixFS for directory traversal
    const cid = await ipfs.dag.put(newNode, {
        storeCodec: "dag-pb",
        hashAlg: "sha2-256"
    }).then(decodeCID);
    await attemptPin(cid);
    const nodeSize = await size(cid);
    return {
        cid,
        size: nodeSize,
        isFile: false
    };
};
export const dagPutLinks = async (links) => {
    const node = dagPB.createNode(DAG_NODE_DATA, links);
    return dagPut(node);
};
export const size = async (cid) => {
    const newCID = typeCheckCID(cid);
    const ipfs = await getIpfs();
    const stat = await ipfs.files.stat(`/ipfs/${newCID}`);
    return stat.cumulativeSize;
};
const attemptPin = async (cid) => {
    if (!setup.shouldPin)
        return;
    const ipfs = await getIpfs();
    try {
        await ipfs.pin.add(cid, { recursive: false });
    }
    catch (err) {
        if (!isObject(err))
            throw err;
        if (!isString(err.message))
            throw err;
        if (!err.message || !err.message.includes("already pinned recursively")) {
            throw err;
        }
    }
};
const typeCheckCID = (cid) => {
    const newCID = CID.asCID(cid);
    if (!newCID)
        throw new Error(`Expected a CID class instance: Found ${cid}`);
    return newCID;
};
//# sourceMappingURL=basic.js.map