
import * as path from "path";
import { DataNativeBinding } from "../types";
import { NodeTrustedMailcore2NativeBinding } from '../types/NodeTrustedMailcore2NativeBinding'

// tslint:disable-next-line: no-var-requires
let bindings: NodeTrustedMailcore2NativeBinding;
try {
    bindings = require(path.join(__dirname, "../../lib/binding/trusted_mailcore2.node"));
} catch(e) {
    bindings = require(path.join(__dirname, "../../lib/binding/trusted_mailcore2_no_crypto.node"));
}

// tslint:disable-next-line
const { Data: _Data } = bindings

/**
 * Data Class
 *
 * @public
 */
class Data extends bindings.Data {
    public handle: DataNativeBinding

    constructor(bufData?: Buffer) {
        super()

        const handle = new _Data(bufData);
        this.handle = handle;
    }

    /**
     * Length of data
     *
     * @memberof Data
     */
    public length(): number {
        return this.handle?.length() || 0;
    }

    /**
     * Bytes of data
     *
     * @memberof Data
     */
    public bytes(): Buffer {
        return this.handle.bytes();
    }

    /**
     * Convert data to BASE64 string
     *
     * @memberof Data
     */
    public base64String(): string {
        return this.handle.base64String();
    }

    /**
     * Write data to file
     *
     * @memberof Data
     */
    public writeToFile(filename: string): number {
        return this.handle.writeToFile(filename);
    }
}

export { Data }
