var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var MintblueWriter_1;
import { Mintblue } from '../mintblue.js';
import { LogClass } from './utils.js';
/**
 * An implementation of the Writer interface that integrates with the mintBlue SDK.
 * This writer sends transactional data to the mintBlue platform.
 *
 * @category Machine
 */
let MintblueWriter = MintblueWriter_1 = class MintblueWriter {
    mintblue;
    project_id;
    /**
     * Factory method to create an instance of the MintblueWriter.
     * Initializes the mintBlue SDK and returns a new writer instance.
     *
     * @param sdkToken - The SDK token for authentication with mintBlue.
     * @param project_id - The project identifier in mintBlue where the data will be written.
     * @returns A promise that resolves to a new MintblueWriter instance.
     */
    static async create(sdkToken, project_id) {
        const mintblue = await Mintblue.create({ token: sdkToken });
        return new MintblueWriter_1(mintblue, project_id);
    }
    /**
     * Constructor for MintblueWriter.
     *
     * @param mintblue - An instance of the mintBlue SDK.
     * @param project_id - The project identifier in mintBlue where the data will be written.
     */
    constructor(mintblue, project_id) {
        this.mintblue = mintblue;
        this.project_id = project_id;
    }
    /**
     * Writes the given action as a transaction to the mintBlue platform.
     *
     * @param action - The action to write.
     * @throws Will throw an error if the mintBlue SDK has not been initialized.
     * @returns A promise that resolves when the write operation completes or rejects on error.
     */
    async write(action) {
        if (!this.mintblue) {
            throw new Error('mintBlue SDK not initialized yet');
        }
        try {
            await this.mintblue.createTransaction({
                project_id: this.project_id,
                outputs: [{ type: 'data', value: { f: action.f, args: action.args }, encrypt: true, sign: true }],
            });
        }
        catch (e) {
            this.handleError(e);
        }
    }
    /**
     * Handles and logs errors from the write operations.
     *
     * @param error - The error to handle and log.
     */
    handleError(error) {
        console.log('MintblueWriter Error ' + error);
    }
};
MintblueWriter = MintblueWriter_1 = __decorate([
    LogClass()
], MintblueWriter);
export { MintblueWriter };
//# sourceMappingURL=MintblueWriter.js.map