"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Holder_1 = require("../../../../../Class/Holder");
const ArrayBuffer_1 = require("../../../../../wrap/ArrayBuffer");
const _ids_1 = require("../../../../_ids");
class Encrypt extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['opt', 'algorithm', 'key', 'data'],
            fo: ['data'],
        }, {
            input: {
                algorithm: {
                    ref: true,
                },
                key: {
                    ref: true,
                },
                data: {
                    ref: true,
                },
            },
            output: {
                data: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_ENCRYPT);
    }
    async f({ key, algorithm, data }, done, fail) {
        const { api: { crypto: { encrypt }, }, } = this.__system;
        const key_ = (await key.raw());
        const algorithm_ = (await algorithm.raw());
        const data_ = (await data.raw());
        let _data;
        try {
            _data = (await encrypt(algorithm_, key_, data_));
        }
        catch (err) {
            if (err.message) {
                fail(err.message.toLowerCase());
            }
            else {
                if (err.name === 'OperationError') {
                    fail('encrypt operation error');
                }
                else {
                    fail('encrypt failed for an unknown reason');
                }
            }
            return;
        }
        const __data = (0, ArrayBuffer_1.wrapArrayBuffer)(_data, this.__system);
        done({ data: __data });
    }
}
exports.default = Encrypt;
//# sourceMappingURL=index.js.map