/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

import { Operation } from "./enums";
import { Transaction, QueryObject } from "./interfaces";

export default class AGCCloudDBTransaction {
    private readonly transactions: Transaction[];
    private constructor() {
        this.transactions = [];
    }

    /**
     * Initializes an AGCCloudDBTransaction object.
     * @param zoneName Cloud DB zone name
     */
    static function() {
        return new AGCCloudDBTransaction();
    }

    /**
     * Writes a group of objects to the Cloud DB zone in a transaction in batches.
     * @param className A class object that represents the object type entity class defined by the developer.
     * @param data A list, represents the data to be written.
     */
    executeUpsert(className: string, data: any[]) {
        this.transactions.push({
            operation: Operation.UPSERT,
            className: className,
            data: data
        })
        return this
    }

    /**
     * Deletes a group of objects from the Cloud DB zone in a transaction in batches.
     * @param className A class object that represents the object type entity class defined by the developer.
     * @param data A list, represents the data to be deleted.
     */
    executeDelete(className: string, data: any[]) {
        this.transactions.push({
            operation: Operation.DELETE,
            className: className,
            data: data
        })
        return this
    }

    /**
     * Queries a set of objects that meet specific conditions from the Cloud DB zone on the cloud in a transaction.
     * @param className A class object that represents the object type entity class defined by the developer.
     * @param data A QueryObject object, which indicates the query condition.
     */
    executeQuery(className: string, data: QueryObject) {
        this.transactions.push({
            operation: Operation.QUERY,
            className: className,
            data: data
        })
        return this
    }

    /**
     * This method is used to build the Transaction list.
     * @returns Transaction list.
     */
    build() {
        Object.freeze(this);
        return this.transactions;
    }
}