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 EndpointCreatorApiConsumer extends BaseCreatorApiConsumer {
    constructor({ baseUrl, instance }, params = {}) {
        super(params);
        this.instance = instance;
        this.baseUrl = baseUrl;
        if (params.nestedUrl)
            this.nestedUrl = params.nestedUrl;
    }
    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);
        }
        if (this.fieldsToSend) {
            itemCopy = sanitizer(itemCopy, this.fieldsToSend);
        }
        const baseUrl = `${this.baseUrl}/${parentId}/${this.nestedUrl}`;
        return this._createItem(itemCopy, baseUrl);
    }
    _createItem(item, baseUrl = this.baseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const response = yield this.instance.post(baseUrl, item);
                return this.responseHandler(response);
            }
            catch (err) {
                throw err;
            }
        });
    }
}
//# sourceMappingURL=endpoint-creator-api-consumer.js.map