import { __awaiter } from "tslib";
import dcopy from 'deep-copy';
import sanitizer from '../../_shared/utils/sanitizer';
import BaseCreatorApiConsumer from '../base-creator-api-consumer/base-creator-api-consumer';
export default class SdkCreatorApiConsumer extends BaseCreatorApiConsumer {
    constructor(SDKMethod, params) {
        super(params);
        this.SDKMethod = SDKMethod;
    }
    createItem({ itemInstance }) {
        let itemCopy = dcopy(itemInstance);
        if (this.preRequestHandler) {
            itemCopy = this.preRequestHandler(itemCopy);
        }
        if (this.fieldsToSend) {
            itemCopy = sanitizer(itemCopy, this.fieldsToSend);
        }
        return this._createItem([itemCopy]);
    }
    createNestedItem({ parentId, itemInstance, }) {
        let itemCopy = dcopy(itemInstance);
        if (this.preRequestHandler) {
            itemCopy = this.preRequestHandler(itemCopy, parentId);
        }
        if (this.fieldsToSend) {
            itemCopy = sanitizer(itemCopy, this.fieldsToSend);
        }
        return this._createItem([parentId, itemCopy]);
    }
    _createItem(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-creator-api-consumer.js.map