import { CommandBase } from '../validation/commandBase.js';
import { isValidStringProperty } from '../validation/string.js';
export default class Merger extends CommandBase {
    className;
    consistencyLevel;
    id;
    objectsPath;
    properties;
    tenant;
    constructor(client, objectsPath) {
        super(client);
        this.objectsPath = objectsPath;
    }
    withProperties = (properties) => {
        this.properties = properties;
        return this;
    };
    withClassName = (className) => {
        this.className = className;
        return this;
    };
    withId = (id) => {
        this.id = id;
        return this;
    };
    withConsistencyLevel = (cl) => {
        this.consistencyLevel = cl;
        return this;
    };
    withTenant = (tenant) => {
        this.tenant = tenant;
        return this;
    };
    validateClassName = () => {
        if (!isValidStringProperty(this.className)) {
            this.addError('className must be set - set with withClassName(className)');
        }
    };
    validateId = () => {
        if (this.id == undefined || this.id == null || this.id.length == 0) {
            this.addError('id must be set - set with withId(id)');
        }
    };
    payload = () => ({
        tenant: this.tenant,
        properties: this.properties,
        class: this.className,
        id: this.id,
    });
    validate = () => {
        this.validateClassName();
        this.validateId();
    };
    do = () => {
        this.validate();
        if (this.errors.length > 0) {
            return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
        }
        return this.objectsPath
            .buildMerge(this.id, this.className, this.consistencyLevel)
            .then((path) => this.client.patch(path, this.payload()));
    };
}
