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 { replace } from "./fable_modules/fable-library-js.4.26.0/RegExp.js";
import { defaultArg } from "./fable_modules/fable-library-js.4.26.0/Option.js";
import { append, contains } from "./fable_modules/fable-library-js.4.26.0/Array.js";
import { stringHash } from "./fable_modules/fable-library-js.4.26.0/Util.js";
import { SemanticReleaseError_$ctor_Z2FC25A28 } from "./Errors.js";

const versionRegex = /^version\s*=\s*(.*)/gum;

export function prepare(api, config, context) {
    return singleton.Delay(() => {
        context.logger.info(concat("Write new release version (", context.nextRelease.version, ...") into Cargo.toml."));
        return singleton.Bind(api.readFile("./Cargo.toml"), (_arg) => {
            const tomlContent_1 = replace(versionRegex, _arg, concat("version = \"", context.nextRelease.version, ..."\""));
            return singleton.Bind(api.writeFile("./Cargo.toml", tomlContent_1), () => {
                let args, allFeatures, args_1, matchValue, arg_2;
                return singleton.Combine(defaultArg(config.check, true) ? ((context.logger.info("Run cargo check."), (args = defaultArg(config.checkArgs, []), (allFeatures = defaultArg(config.allFeatures, false), (args_1 = ((matchValue = contains("--all-features", args, {
                    Equals: (x, y) => (x === y),
                    GetHashCode: stringHash,
                }), matchValue ? args : (allFeatures ? append(args, ["--all-features"]) : args))), singleton.Bind((arg_2 = append(["check"], args_1), api.exec(arg_2)), (_arg_2) => {
                    const err = _arg_2[1];
                    if (_arg_2[2] !== 0) {
                        context.logger.error(concat("Cargo check failed: ", ...err));
                        throw SemanticReleaseError_$ctor_Z2FC25A28("Cargo check failed.", "ECARGOCHECK", err);
                        return singleton.Zero();
                    }
                    else {
                        return singleton.Zero();
                    }
                })))))) : singleton.Zero(), singleton.Delay(() => {
                    return singleton.Zero();
                }));
            });
        });
    });
}

