"use strict";
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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorActor = exports.EchoActor = exports.TestActor = exports.expectError = void 0;
const base_1 = require("@darlean/base");
const utils_1 = require("@darlean/utils");
async function expectError(handler) {
    let error;
    try {
        const result = await handler();
        console.log('EXPECT ERROR RESULT', result);
    }
    catch (e) {
        error = e;
        return e;
    }
    expect(error).toBeDefined();
}
exports.expectError = expectError;
class TestActor {
    constructor(actor) {
        this.actor = actor;
        // eslint-disable-next-line @typescript-eslint/no-this-alias
        const self = this;
        this.proxy = new Proxy(actor, {
            get(_target, p, _receiver) {
                return async function (...args) {
                    return await self.handleCall(p.toString(), args);
                };
            }
        });
    }
    async handleCall(name, args) {
        if (this.disconnected) {
            throw new Error('Disconnected');
        }
        if (this.preDelay) {
            await (0, utils_1.sleep)(this.preDelay);
        }
        try {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
            return await this.actor[name].apply(this.actor, args);
        }
        finally {
            if (this.postDelay) {
                await (0, utils_1.sleep)(this.postDelay);
            }
        }
    }
}
exports.TestActor = TestActor;
class EchoActor {
    constructor(persistence, defaultLast, store = true) {
        this.last = persistence.persistable(['last'], undefined, defaultLast);
        this.store = store;
    }
    async echo(value) {
        await (0, utils_1.sleep)(50);
        this.last.change(value);
        return value;
    }
    async getLastValue() {
        await (0, utils_1.sleep)(50);
        return this.last.value;
    }
    async activate() {
        if (this.store) {
            await this.last.load();
        }
    }
    async deactivate() {
        if (this.store) {
            await this.last.store();
        }
    }
}
__decorate([
    (0, base_1.action)({ locking: 'exclusive' }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], EchoActor.prototype, "echo", null);
__decorate([
    (0, base_1.action)({ locking: 'exclusive' }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], EchoActor.prototype, "getLastValue", null);
exports.EchoActor = EchoActor;
class ErrorActor {
    async error(msg) {
        throw new Error(msg);
    }
}
__decorate([
    (0, base_1.action)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], ErrorActor.prototype, "error", null);
exports.ErrorActor = ErrorActor;
