"use strict";
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for (var i = 0; i < initializers.length; i++) {
        value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    }
    return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for (var i = decorators.length - 1; i >= 0; i--) {
        var context = {};
        for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
        for (var p in contextIn.access) context.access[p] = contextIn.access[p];
        context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
        var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        }
        else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
    if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
    return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BulkFindOp = void 0;
const decorators_1 = require("./decorators");
const errors_1 = require("@mongosh/errors");
const enums_1 = require("./enums");
const helpers_1 = require("./helpers");
const result_1 = require("./result");
let BulkFindOp = (() => {
    let _classDecorators = [decorators_1.shellApiClassDefault];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = decorators_1.ShellApiWithMongoClass;
    let _instanceExtraInitializers = [];
    let _collation_decorators;
    let _arrayFilters_decorators;
    let _hint_decorators;
    let _delete_decorators;
    let _deleteOne_decorators;
    let _remove_decorators;
    let _removeOne_decorators;
    let _replaceOne_decorators;
    let _updateOne_decorators;
    let _update_decorators;
    let _upsert_decorators;
    var BulkFindOp = _classThis = class extends _classSuper {
        constructor(innerFind, parentBulk) {
            super();
            this._serviceProviderBulkFindOp = __runInitializers(this, _instanceExtraInitializers);
            this._serviceProviderBulkFindOp = innerFind;
            this._parentBulk = parentBulk;
        }
        get _mongo() {
            return this._parentBulk._mongo;
        }
        [enums_1.asPrintable]() {
            return 'BulkFindOp';
        }
        collation(spec) {
            this._serviceProviderBulkFindOp.collation(spec);
            return this;
        }
        arrayFilters(filters) {
            this._serviceProviderBulkFindOp.arrayFilters(filters);
            return this;
        }
        hint(hintDoc) {
            (0, helpers_1.assertArgsDefinedType)([hintDoc], [true], 'BulkFindOp.hint');
            this._serviceProviderBulkFindOp.hint(hintDoc);
            return this;
        }
        delete() {
            this._parentBulk._batchCounts.nRemoveOps++;
            this._serviceProviderBulkFindOp.delete();
            return this._parentBulk;
        }
        deleteOne() {
            this._parentBulk._batchCounts.nRemoveOps++;
            this._serviceProviderBulkFindOp.deleteOne();
            return this._parentBulk;
        }
        remove() {
            return this.delete();
        }
        removeOne() {
            return this.deleteOne();
        }
        replaceOne(replacement) {
            this._parentBulk._batchCounts.nUpdateOps++;
            (0, helpers_1.assertArgsDefinedType)([replacement], [true], 'BulkFindOp.replacement');
            const op = (0, helpers_1.shallowClone)(replacement);
            this._serviceProviderBulkFindOp.replaceOne(op);
            return this._parentBulk;
        }
        updateOne(update) {
            this._parentBulk._batchCounts.nUpdateOps++;
            (0, helpers_1.assertArgsDefinedType)([update], [true], 'BulkFindOp.update');
            const op = (0, helpers_1.shallowClone)(update);
            this._serviceProviderBulkFindOp.updateOne(op);
            return this._parentBulk;
        }
        update(update) {
            this._parentBulk._batchCounts.nUpdateOps++;
            (0, helpers_1.assertArgsDefinedType)([update], [true], 'BulkFindOp.update');
            const op = (0, helpers_1.shallowClone)(update);
            this._serviceProviderBulkFindOp.update(op);
            return this._parentBulk;
        }
        upsert() {
            this._serviceProviderBulkFindOp.upsert();
            return this;
        }
    };
    __setFunctionName(_classThis, "BulkFindOp");
    (() => {
        const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
        _collation_decorators = [(0, decorators_1.returnType)('BulkFindOp'), (0, decorators_1.apiVersions)([1])];
        _arrayFilters_decorators = [(0, decorators_1.returnType)('BulkFindOp'), (0, decorators_1.apiVersions)([1])];
        _hint_decorators = [(0, decorators_1.returnType)('BulkFindOp'), (0, decorators_1.apiVersions)([1])];
        _delete_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1])];
        _deleteOne_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1])];
        _remove_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1]), decorators_1.deprecated];
        _removeOne_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1]), decorators_1.deprecated];
        _replaceOne_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1])];
        _updateOne_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1])];
        _update_decorators = [(0, decorators_1.returnType)('Bulk')];
        _upsert_decorators = [(0, decorators_1.returnType)('Bulk')];
        __esDecorate(_classThis, null, _collation_decorators, { kind: "method", name: "collation", static: false, private: false, access: { has: obj => "collation" in obj, get: obj => obj.collation }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _arrayFilters_decorators, { kind: "method", name: "arrayFilters", static: false, private: false, access: { has: obj => "arrayFilters" in obj, get: obj => obj.arrayFilters }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _hint_decorators, { kind: "method", name: "hint", static: false, private: false, access: { has: obj => "hint" in obj, get: obj => obj.hint }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _delete_decorators, { kind: "method", name: "delete", static: false, private: false, access: { has: obj => "delete" in obj, get: obj => obj.delete }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _deleteOne_decorators, { kind: "method", name: "deleteOne", static: false, private: false, access: { has: obj => "deleteOne" in obj, get: obj => obj.deleteOne }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _remove_decorators, { kind: "method", name: "remove", static: false, private: false, access: { has: obj => "remove" in obj, get: obj => obj.remove }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _removeOne_decorators, { kind: "method", name: "removeOne", static: false, private: false, access: { has: obj => "removeOne" in obj, get: obj => obj.removeOne }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _replaceOne_decorators, { kind: "method", name: "replaceOne", static: false, private: false, access: { has: obj => "replaceOne" in obj, get: obj => obj.replaceOne }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _updateOne_decorators, { kind: "method", name: "updateOne", static: false, private: false, access: { has: obj => "updateOne" in obj, get: obj => obj.updateOne }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _update_decorators, { kind: "method", name: "update", static: false, private: false, access: { has: obj => "update" in obj, get: obj => obj.update }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _upsert_decorators, { kind: "method", name: "upsert", static: false, private: false, access: { has: obj => "upsert" in obj, get: obj => obj.upsert }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
        BulkFindOp = _classThis = _classDescriptor.value;
        if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        __runInitializers(_classThis, _classExtraInitializers);
    })();
    return BulkFindOp = _classThis;
})();
exports.BulkFindOp = BulkFindOp;
let Bulk = (() => {
    let _classDecorators = [decorators_1.shellApiClassDefault];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = decorators_1.ShellApiWithMongoClass;
    let _instanceExtraInitializers = [];
    let _execute_decorators;
    let _find_decorators;
    let _insert_decorators;
    var Bulk = _classThis = class extends _classSuper {
        constructor(collection, innerBulk, ordered = false) {
            super();
            this._mongo = __runInitializers(this, _instanceExtraInitializers);
            this._collection = collection;
            this._mongo = collection._mongo;
            this._serviceProviderBulkOp = innerBulk;
            this._batchCounts = {
                nInsertOps: 0,
                nUpdateOps: 0,
                nRemoveOps: 0,
            };
            this._executed = false;
            this._ordered = ordered;
        }
        [enums_1.asPrintable]() {
            return this.toJSON();
        }
        _emitBulkApiCall(methodName, methodArguments = {}) {
            this._mongo._instanceState.emitApiCallWithArgs({
                method: methodName,
                class: 'Bulk',
                db: this._collection._database._name,
                coll: this._collection._name,
                arguments: methodArguments,
            });
        }
        async execute(writeConcern) {
            const result = await this._serviceProviderBulkOp.execute();
            this._executed = true;
            this._emitBulkApiCall('execute', { writeConcern: writeConcern });
            return new result_1.BulkWriteResult(!!result.ok, result.insertedCount, result.insertedIds, result.matchedCount, result.modifiedCount, result.deletedCount, result.upsertedCount, result.upsertedIds);
        }
        find(query) {
            (0, helpers_1.assertArgsDefinedType)([query], [true], 'Bulk.find');
            return new BulkFindOp(this._serviceProviderBulkOp.find(query), this);
        }
        insert(document) {
            this._batchCounts.nInsertOps++;
            (0, helpers_1.assertArgsDefinedType)([document], [true], 'Bulk.insert');
            this._serviceProviderBulkOp.insert(document);
            return this;
        }
        toJSON() {
            const batches = this._serviceProviderBulkOp.batches.length;
            return {
                ...this._batchCounts,
                nBatches: batches,
            };
        }
        toString() {
            return JSON.stringify(this.toJSON());
        }
        getOperations() {
            if (!this._executed) {
                throw new errors_1.MongoshInvalidInputError('Cannot call getOperations on an unexecuted Bulk operation', errors_1.CommonErrors.InvalidOperation);
            }
            return this._serviceProviderBulkOp.batches.map((b) => ({
                originalZeroIndex: b.originalZeroIndex,
                batchType: b.batchType,
                operations: b.operations,
            }));
        }
    };
    __setFunctionName(_classThis, "Bulk");
    (() => {
        const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
        _execute_decorators = [decorators_1.returnsPromise, (0, decorators_1.apiVersions)([1])];
        _find_decorators = [(0, decorators_1.returnType)('BulkFindOp'), (0, decorators_1.apiVersions)([1])];
        _insert_decorators = [(0, decorators_1.returnType)('Bulk'), (0, decorators_1.apiVersions)([1])];
        __esDecorate(_classThis, null, _execute_decorators, { kind: "method", name: "execute", static: false, private: false, access: { has: obj => "execute" in obj, get: obj => obj.execute }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _find_decorators, { kind: "method", name: "find", static: false, private: false, access: { has: obj => "find" in obj, get: obj => obj.find }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(_classThis, null, _insert_decorators, { kind: "method", name: "insert", static: false, private: false, access: { has: obj => "insert" in obj, get: obj => obj.insert }, metadata: _metadata }, null, _instanceExtraInitializers);
        __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
        Bulk = _classThis = _classDescriptor.value;
        if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        __runInitializers(_classThis, _classExtraInitializers);
    })();
    return Bulk = _classThis;
})();
exports.default = Bulk;
//# sourceMappingURL=bulk.js.map