var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Buffer } from 'buffer';
import { getContentType, getExtension, randomStr } from '../../utils';
import { InvalidJsonVariableError } from '../../errors';
export const toMetaplexFile = (content, fileName, options = {}) => {
    var _a, _b, _c, _d, _e;
    return ({
        buffer: parseMetaplexFileContent(content),
        fileName,
        displayName: (_a = options.displayName) !== null && _a !== void 0 ? _a : fileName,
        uniqueName: (_b = options.uniqueName) !== null && _b !== void 0 ? _b : randomStr(),
        contentType: (_c = options.contentType) !== null && _c !== void 0 ? _c : getContentType(fileName),
        extension: (_d = options.extension) !== null && _d !== void 0 ? _d : getExtension(fileName),
        tags: (_e = options.tags) !== null && _e !== void 0 ? _e : [],
    });
};
export const toMetaplexFileFromBrowser = (file, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
    const buffer = yield file.arrayBuffer();
    return toMetaplexFile(buffer, file.name, options);
});
export const toMetaplexFileFromJson = (json, fileName = 'inline.json', options = {}) => {
    let jsonString;
    try {
        jsonString = JSON.stringify(json);
    }
    catch (error) {
        throw new InvalidJsonVariableError({ cause: error });
    }
    return toMetaplexFile(jsonString, fileName, options);
};
export const parseMetaplexFileContent = (content) => {
    if (content instanceof ArrayBuffer) {
        return Buffer.from(new Uint8Array(content));
    }
    return Buffer.from(content);
};
export const getBytesFromMetaplexFiles = (...files) => files.reduce((acc, file) => acc + file.buffer.byteLength, 0);
export const getBrowserFileFromMetaplexFile = (file) => new File([file.buffer], file.fileName);
export const isMetaplexFile = (metaplexFile) => {
    return (metaplexFile != null &&
        typeof metaplexFile === 'object' &&
        'buffer' in metaplexFile &&
        'fileName' in metaplexFile &&
        'displayName' in metaplexFile &&
        'uniqueName' in metaplexFile &&
        'contentType' in metaplexFile &&
        'extension' in metaplexFile &&
        'tags' in metaplexFile);
};
//# sourceMappingURL=MetaplexFile.js.map