var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { get, post } from './requests';
import state from 'src/state';
export function getCustomizedModel(sku, customization) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const url = `/viewer-customization/customer/${state.customer._id}/products/${sku}?customization=${encodeURI(JSON.stringify(customization))}`;
            const customizedModel = (yield get(url)) || {};
            return customizedModel;
        }
        catch (e) {
            console.error(`Error on getting customized model: ${e.message}`);
            return Promise.reject(e);
        }
    });
}
export function uploadCustomization(sku, customization, customizationModel) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const url = `/viewer-customization/customer/${state.customer._id}/products/${sku}/upload-customization-data?customization=${encodeURI(JSON.stringify(customization))}`;
            const signedUpload = (yield get(url));
            const file = new File([customizationModel], signedUpload.filename);
            const formData = new FormData();
            Object.keys(signedUpload.fields).forEach((field) => formData.append(field, signedUpload.fields[field]));
            formData.append('file', file);
            yield fetch(signedUpload.url, {
                method: 'POST',
                body: formData,
            });
            return signedUpload.url.concat(signedUpload.fields['key']);
        }
        catch (e) {
            console.error(`Error on upload customization: ${e.message}`);
            return Promise.reject(e);
        }
    });
}
export function requestConversion(sku, filename) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const url = `/viewer-customization/customers/${state.customer._id}/products/${sku}/convert`;
            const response = yield post(url, { filename });
            return response;
        }
        catch (e) {
            console.error(`Error on request sku conversion: ${e.message}`);
            return Promise.reject(e);
        }
    });
}
export function retryCheckConversion(jobId) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const { status } = (yield get(`/job-status/${jobId}`));
            if (['waiting', 'processing'].includes(status))
                return retryCheckConversion(jobId);
            return status;
        }
        catch (e) {
            console.error(`Error on retry check conversion: ${e.message}`);
            return Promise.reject(e);
        }
    });
}
//# sourceMappingURL=customization.js.map