import { __awaiter } from "tslib";
import dcopy from 'deep-copy';
import sanitizer from '../../_shared/utils/sanitizer';
import BaseUpdaterApiConsumer from '../base-updater-api-consumer/base-updater-api-consumer';
export default class SdkUpdaterApiConsumer extends BaseUpdaterApiConsumer {
    constructor(SDKMethod, params = {}) {
        super(params);
        this.SDKMethod = SDKMethod;
    }
    updateItem({ itemId, itemInstance, }) {
        let itemCopy = dcopy(itemInstance);
        if (this.preRequestHandler) {
            itemCopy = this.preRequestHandler(itemCopy);
        }
        if (this.fieldsToSend) {
            itemCopy = sanitizer(itemCopy, this.fieldsToSend);
        }
        return this._updateItem([itemId, itemCopy]);
    }
    updateNestedItem({ parentId, itemId, itemInstance, }) {
        let itemCopy = dcopy(itemInstance);
        if (this.preRequestHandler) {
            itemCopy = this.preRequestHandler(itemCopy, parentId);
        }
        if (this.fieldsToSend) {
            itemCopy = sanitizer(itemCopy, this.fieldsToSend);
        }
        return this._updateItem([parentId, itemId, itemCopy]);
    }
    _updateItem(args) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                // @ts-ignore
                const response = yield this.SDKMethod(...args);
                return this.responseHandler(response);
            }
            catch (err) {
                throw err;
            }
        });
    }
}
//# sourceMappingURL=sdk-updater-api-consumer.js.map