import { CommandBase } from '../validation/commandBase.js';
import { isValidStringProperty } from '../validation/string.js';
import { updateShard } from './shardUpdater.js';
import { getShards } from './shardsGetter.js';
export default class ShardsUpdater extends CommandBase {
    className;
    shards;
    status;
    constructor(client) {
        super(client);
        this.shards = [];
    }
    withClassName = (className) => {
        this.className = className;
        return this;
    };
    validateClassName = () => {
        if (!isValidStringProperty(this.className)) {
            this.addError('className must be set - set with .withClassName(className)');
        }
    };
    withStatus = (status) => {
        this.status = status;
        return this;
    };
    validateStatus = () => {
        if (!isValidStringProperty(this.status)) {
            this.addError('status must be set - set with .withStatus(status)');
        }
    };
    validate = () => {
        this.validateClassName();
        this.validateStatus();
    };
    updateShards = async () => {
        const payload = await Promise.all(Array.from({ length: this.shards.length }, (_, i) => updateShard(this.client, this.className, this.shards[i].name || '', this.status)
            .then((res) => {
            return { name: this.shards[i].name, status: res.status };
        })
            .catch((err) => this.addError(err.toString()))));
        if (this.errors.length > 0) {
            return Promise.reject(new Error(`failed to update shards: ${this.errors.join(', ')}`));
        }
        return Promise.resolve(payload);
    };
    do = () => {
        this.validate();
        if (this.errors.length > 0) {
            return Promise.reject(new Error(`invalid usage: ${this.errors.join(', ')}`));
        }
        return getShards(this.client, this.className)
            .then((shards) => (this.shards = shards))
            .then(() => {
            return this.updateShards();
        })
            .then((payload) => {
            return payload;
        })
            .catch((err) => {
            return Promise.reject(err);
        });
    };
}
