import { __awaiter } from "tslib";
import dcopy from 'deep-copy';
import sanitizer from '../../_shared/utils/sanitizer';
import BasePatcherApiConsumer from '../base-patcher-api-consumer/base-patcher-api-consumer';
export default class SdkPatcherApiConsumer extends BasePatcherApiConsumer {
    constructor(SDKMethod, params = {}) {
        super(params);
        this.SDKMethod = SDKMethod;
    }
    patchItem({ id, changes }) {
        let changesCopy = dcopy(changes);
        if (this.preRequestHandler) {
            changesCopy = this.preRequestHandler(changesCopy);
        }
        if (this.fieldsToSend) {
            changesCopy = sanitizer(changesCopy, this.fieldsToSend);
        }
        return this._patchItem([id, changesCopy]);
    }
    patchNestedItem({ parentId, id, changes, }) {
        let changesCopy = dcopy(changes);
        if (this.preRequestHandler) {
            changesCopy = this.preRequestHandler(changesCopy, parentId);
        }
        if (this.fieldsToSend) {
            changesCopy = sanitizer(changesCopy, this.fieldsToSend);
        }
        return this._patchItem([parentId, id, changesCopy]);
    }
    _patchItem(args) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                // @ts-ignore
                const response = yield this.SDKMethod(...args);
                return response;
            }
            catch (err) {
                throw err;
            }
        });
    }
}
//# sourceMappingURL=sdk-patcher-api-consumer.js.map