"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Result = void 0;
const errors_1 = require("./errors");
var Result;
(function (Result) {
    function ok(value) {
        return new ResultOk(value);
    }
    Result.ok = ok;
    function error(value) {
        return new ResultError(value);
    }
    Result.error = error;
    function fromString(errorMessage) {
        return Result.error(new Error(errorMessage));
    }
    Result.fromString = fromString;
    function tryWith(f) {
        try {
            return Result.ok(f());
        }
        catch (err) {
            return Result.error(errors_1.ErrorUtils.fromUnknown(err));
        }
    }
    Result.tryWith = tryWith;
    async function tryWithAsync(f) {
        try {
            return Result.ok(await f());
        }
        catch (err) {
            return Result.error(errors_1.ErrorUtils.fromUnknown(err));
        }
    }
    Result.tryWithAsync = tryWithAsync;
})(Result || (exports.Result = Result = {}));
/**
 * To instantiate a ResultOk, use `Result.ok(value)`.
 * To instantiate a ResultError, use `Result.error(value)`.
 */
class ResultOk {
    constructor(val) {
        this.val = val;
    }
    map(f) {
        return new ResultOk(f(this.val));
    }
    mapError(_f) {
        return this;
    }
    flatMap(f) {
        return f(this.val);
    }
    /**
     * Returns the contained ok value.
     * @throws if this is an error (which is impossible for `ResultOk`,
     *   but provided for use on the `Result<T, E>` union type).
     */
    unwrap() {
        return this.val;
    }
    /**
     * Returns the contained ok value, or the provided default if this
     * is an error.
     */
    unwrapOr(_defaultValue) {
        return this.val;
    }
    isOk() {
        return true;
    }
    isError() {
        return false;
    }
}
/**
 * To instantiate a ResultOk, use `Result.ok(value)`.
 * To instantiate a ResultError, use `Result.error(value)`.
 */
class ResultError {
    constructor(err) {
        this.err = err;
    }
    map(_f) {
        return this;
    }
    mapError(f) {
        return new ResultError(f(this.err));
    }
    flatMap(_f) {
        return this;
    }
    /**
     * Always throws since this is an error result.
     * @throws The contained error value (wrapped in Error if not already one).
     */
    unwrap() {
        if (this.err instanceof Error) {
            throw this.err;
        }
        throw errors_1.ErrorUtils.fromUnknown(this.err);
    }
    /**
     * Returns the provided default value since this is an error.
     */
    unwrapOr(defaultValue) {
        return defaultValue;
    }
    isOk() {
        return false;
    }
    isError() {
        return true;
    }
}
//# sourceMappingURL=result.js.map