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 EndpointUpdaterApiConsumer extends BaseUpdaterApiConsumer {
    constructor({ baseUrl, instance }, params = {}) {
        super(params);
        this.instance = instance;
        this.baseUrl = baseUrl;
        if (params.nestedUrl)
            this.nestedUrl = params.nestedUrl;
    }
    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({ id: itemId, item: itemCopy });
    }
    updateNestedItem({ parentId, itemId, 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._updateItem({ id: itemId, item: itemCopy }, baseUrl);
    }
    async _updateItem({ id, item, }, baseUrl = this.baseUrl) {
        const updUrl = `${baseUrl}/${id}`;
        try {
            const response = await this.instance.put(updUrl, item);
            return this.responseHandler(response);
        }
        catch (err) {
            throw err;
        }
    }
}
//# sourceMappingURL=endpoint-updater-api-consumer.js.map