"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Result = void 0;
class Result {
    res;
    static ok(value) {
        return new Result({ ok: true, value });
    }
    static err(error) {
        if (typeof error === 'undefined') {
            return new Result({ ok: false, error: new Error() });
        }
        if (typeof error === 'string') {
            return new Result({ ok: false, error: new Error(error) });
        }
        return new Result({ ok: false, error });
    }
    static wrapCallback(callback) {
        try {
            return Result.ok(callback());
        }
        catch (error) {
            return Result.err(error);
        }
    }
    static wrapPromise(promise) {
        return promise.then((value) => Result.ok(value), (error) => Result.err(error));
    }
    static wrap(input) {
        return input instanceof Promise
            ? Result.wrapPromise(input)
            : Result.wrapCallback(input);
    }
    constructor(res) {
        this.res = res;
    }
    transform(fn) {
        return this.res.ok
            ? Result.ok(fn(this.res.value))
            : Result.err(this.res.error);
    }
    unwrap(fallback) {
        if (this.res.ok) {
            return this.res;
        }
        if (arguments.length) {
            return { ok: true, value: fallback };
        }
        return this.res;
    }
    value(fallback) {
        const res = arguments.length ? this.unwrap(fallback) : this.unwrap();
        return res.ok ? res.value : undefined;
    }
    error() {
        return this.res.ok ? undefined : this.res.error;
    }
}
exports.Result = Result;
//# sourceMappingURL=result.js.map