import { singleton } from "./fable_modules/fable-library-js.4.26.0/AsyncBuilder.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 { concat } from "./fable_modules/fable-library-js.4.26.0/String.js";
import { SemanticReleaseError_$ctor_Z2FC25A28 } from "./Errors.js";

export function publish(api, config, context) {
    return singleton.Delay(() => {
        let args, allFeatures, args_1, matchValue, args_2, arg;
        return singleton.Combine(!defaultArg(config.publish, true) ? ((context.logger.warn("Publish set to \'false\'. Skip publishing."), singleton.Zero())) : ((context.logger.info("Publish cargo crate."), (args = defaultArg(config.publishArgs, []), (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))), (args_2 = (contains("--allow-dirty", args_1, {
            Equals: (x_1, y_1) => (x_1 === y_1),
            GetHashCode: stringHash,
        }) ? args_1 : append(args_1, ["--allow-dirty"])), singleton.Bind((arg = append(["publish"], args_2), api.exec(arg)), (_arg) => {
            const err = _arg[1];
            if (_arg[2] !== 0) {
                context.logger.error(concat("Cargo publish failed: ", ...err));
                throw SemanticReleaseError_$ctor_Z2FC25A28("Cargo publish failed.", "ECARGOPUBLISH", err);
                return singleton.Zero();
            }
            else {
                return singleton.Zero();
            }
        }))))))), singleton.Delay(() => {
            return singleton.Zero();
        }));
    });
}

