import { HttpResponseType, HttpStatusCode, Http } from '../../http';
import { toBlob } from '../../blob';
import { clone } from '../../util';
/** See {@link LayerApi} for documentation.  */
export class BimLayerApiClient {
    _apiClient;
    static _httpResponseType = HttpResponseType.status([HttpStatusCode.Ok, HttpStatusCode.Conflict]);
    constructor(_apiClient) {
        this._apiClient = _apiClient;
    }
    async add(attachTo, data) {
        const form = new FormData();
        form.append('layer', JSON.stringify({
            attachToId: attachTo.id,
            format: data.format,
            metadata: this.trimReservedMetadata(data.metadata)
        }));
        if (data.data) {
            form.append('data', toBlob(data.data));
        }
        return await Http.post(`${attachTo.apiUrl}file/layer`, BimLayerApiClient._httpResponseType.json, { body: form });
    }
    async update(layer, options) {
        const headers = new Headers({
            overwrite: (options?.overwrite ?? false).toString()
        });
        const form = new FormData();
        form.append('layer', JSON.stringify({
            id: layer.id,
            etag: layer.etag,
            name: layer.name,
            metadata: this.trimReservedMetadata(layer.metadata)
        }));
        if (options?.data) {
            form.append('data', toBlob(options.data));
        }
        return await Http.put(`${layer.apiUrl}file/layer`, BimLayerApiClient._httpResponseType.json, {
            body: form,
            headers
        });
    }
    /** Removes metadata that can only be set server side to avoid sever side validation errors */
    trimReservedMetadata(metadata) {
        if (metadata?._system) {
            const deepCopyMetadata = clone(metadata);
            delete deepCopyMetadata._system;
            return deepCopyMetadata;
        }
        else if (metadata) {
            return metadata;
        }
    }
}
//# sourceMappingURL=BimLayerApiClient.js.map