import { singleton } from "./fable_modules/fable-library-js.4.26.0/AsyncBuilder.js";
import { concat } from "./fable_modules/fable-library-js.4.26.0/String.js";
import { SemanticReleaseError_$ctor_Z2FC25A28 } from "./Errors.js";
import { defaultArg } from "./fable_modules/fable-library-js.4.26.0/Option.js";

export function verifyConditions(api, config, context) {
    return singleton.Delay(() => {
        context.logger.debug("Check cargo executable and cargo version.");
        return singleton.Combine(singleton.TryWith(singleton.Delay(() => singleton.Bind(api.exec(["--version"]), (_arg) => {
            context.logger.info(concat("Cargo version: ", ..._arg[0]));
            return singleton.Zero();
        })), (_arg_1) => {
            const ex = _arg_1;
            context.logger.error(concat("Failed to check cargo version: ", ...ex.message));
            throw SemanticReleaseError_$ctor_Z2FC25A28(concat("Cargo executable (", api.cargoExecutable, ...") not valid."), "ECARGOEXECUTABLE", ex.message);
            return singleton.Zero();
        }), singleton.Delay(() => singleton.Combine((!defaultArg(config.publish, true) && !defaultArg(config.alwaysVerifyToken, true)) ? ((context.logger.warn("Publish and alwaysVerifyToken set to \'false\'. Skip CARGO_REGISTRY_TOKEN verification."), singleton.Zero())) : singleton.Combine(!(!!context.env["CARGO_REGISTRY_TOKEN"]) ? ((context.logger.error("CARGO_REGISTRY_TOKEN is not set"), ((() => {
            throw SemanticReleaseError_$ctor_Z2FC25A28("CARGO_REGISTRY_TOKEN is not set.", "ENOREGISTRYTOKEN", undefined);
        })(), singleton.Zero()))) : singleton.Zero(), singleton.Delay(() => {
            context.logger.info("Login into registry.");
            return singleton.Bind(api.exec(["login", context.env["CARGO_REGISTRY_TOKEN"]]), (_arg_2) => {
                const err = _arg_2[1];
                if (_arg_2[2] !== 0) {
                    context.logger.error(concat("Failed to login into registry: ", ...err));
                    throw SemanticReleaseError_$ctor_Z2FC25A28("Failed to login into registry.", "ELOGIN", err);
                    return singleton.Zero();
                }
                else {
                    return singleton.Zero();
                }
            });
        })), singleton.Delay(() => singleton.Combine(singleton.TryWith(singleton.Delay(() => singleton.Bind(api.isReadable("./Cargo.toml"), () => singleton.Bind(api.isWritable("./Cargo.toml"), () => singleton.Return(undefined)))), (_arg_5) => {
            context.logger.error("Could not access Cargo.toml file.");
            throw SemanticReleaseError_$ctor_Z2FC25A28("Could not access Cargo.toml file.", "EACCESS", _arg_5.message);
            return singleton.Zero();
        }), singleton.Delay(() => {
            return singleton.Zero();
        }))))));
    });
}

