"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImportService = void 0;
var global_loader_service_1 = require("./../../resources/global-loader/global-loader-service");
var aurelia_framework_1 = require("aurelia-framework");
var aurelia_fetch_client_1 = require("aurelia-fetch-client");
var ImportService = exports.ImportService = (function () {
    function ImportService(httpClient, globalLoader) {
        this.httpClient = httpClient;
        this.globalLoader = globalLoader;
        this.url = "/api/import/";
    }
    ImportService.prototype.startSession = function (context, customParameter) {
        var _a;
        return __awaiter(this, void 0, void 0, function () {
            var startResult, session;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4, this.httpClient.put(this.url + "?" + new URLSearchParams({
                            context: context,
                            customParameter: (_a = customParameter === null || customParameter === void 0 ? void 0 : customParameter.toString()) !== null && _a !== void 0 ? _a : '0'
                        }))];
                    case 1:
                        startResult = _b.sent();
                        if (!startResult.ok) return [3, 3];
                        return [4, startResult.json()];
                    case 2:
                        session = _b.sent();
                        return [2, session];
                    case 3: return [2, null];
                }
            });
        });
    };
    ImportService.prototype.getSession = function (sessionId) {
        return __awaiter(this, void 0, void 0, function () {
            var sessionResult, session;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.get(this.url + sessionId.toString())];
                    case 1:
                        sessionResult = _a.sent();
                        if (!sessionResult.ok) return [3, 3];
                        return [4, sessionResult.json()];
                    case 2:
                        session = _a.sent();
                        return [2, session];
                    case 3: return [2, null];
                }
            });
        });
    };
    ImportService.prototype.saveFileSession = function (id, fileSessions, context) {
        return __awaiter(this, void 0, void 0, function () {
            var saveResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.post(this.url + id + "/saveFile/" + context, JSON.stringify(fileSessions))];
                    case 1:
                        saveResult = _a.sent();
                        if (saveResult.ok) {
                        }
                        return [2, null];
                }
            });
        });
    };
    ImportService.prototype.saveSession = function (session) {
        return __awaiter(this, void 0, void 0, function () {
            var saveResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.post(this.url, JSON.stringify(session))];
                    case 1:
                        saveResult = _a.sent();
                        if (saveResult.ok) {
                        }
                        return [2, null];
                }
            });
        });
    };
    ImportService.prototype.getColumnMapping = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var mappingResult, mapping;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.get(this.url + id + '/mapping')];
                    case 1:
                        mappingResult = _a.sent();
                        if (!mappingResult) return [3, 3];
                        return [4, mappingResult.json()];
                    case 2:
                        mapping = _a.sent();
                        return [2, mapping];
                    case 3: return [2, null];
                }
            });
        });
    };
    ImportService.prototype.getSheetDtos = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var sheetsResult, sheets;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.get(this.url + id + '/sheets')];
                    case 1:
                        sheetsResult = _a.sent();
                        if (!sheetsResult) return [3, 3];
                        return [4, sheetsResult.json()];
                    case 2:
                        sheets = _a.sent();
                        return [2, sheets];
                    case 3: return [2, null];
                }
            });
        });
    };
    ImportService.prototype.getHeaderLabel = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var headerResult, header;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.get(this.url + id + '/labels')];
                    case 1:
                        headerResult = _a.sent();
                        if (!headerResult) return [3, 3];
                        return [4, headerResult.json()];
                    case 2:
                        header = _a.sent();
                        return [2, header];
                    case 3: return [2, null];
                }
            });
        });
    };
    ImportService.prototype.saveMapping = function (id, columnsMapping) {
        return __awaiter(this, void 0, void 0, function () {
            var param, saveMappingResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        columnsMapping.forEach(function (x) {
                            if (x.columnEndIndex == "''" || x.columnEndIndex == null) {
                                x.columnEndIndex = null;
                            }
                            else {
                                x.columnEndIndex = parseInt(x.columnEndIndex.toString());
                            }
                        });
                        param = {
                            ImportColumnMappings: columnsMapping
                        };
                        return [4, this.httpClient.post(this.url + id + "/mapping", JSON.stringify(param))];
                    case 1:
                        saveMappingResult = _a.sent();
                        if (saveMappingResult.ok) {
                        }
                        else {
                            throw "Exception save mapping";
                        }
                        return [2, null];
                }
            });
        });
    };
    ImportService.prototype.saveConfig = function (context, id, config) {
        return __awaiter(this, void 0, void 0, function () {
            var saveMappingResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.post(this.url + context + "/" + id + "/config", JSON.stringify(config))];
                    case 1:
                        saveMappingResult = _a.sent();
                        if (saveMappingResult.ok) {
                        }
                        else {
                            throw "Exception saving config";
                        }
                        return [2, null];
                }
            });
        });
    };
    ImportService.prototype.validateConfig = function (context, id, mapping, config) {
        return __awaiter(this, void 0, void 0, function () {
            var validateMappingResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        mapping.forEach(function (x) {
                            if (x.columnEndIndex == "''" || x.columnEndIndex == null) {
                                x.columnEndIndex = null;
                            }
                            else {
                                x.columnEndIndex = parseInt(x.columnEndIndex.toString());
                            }
                        });
                        return [4, this.httpClient.post(this.url + context + "/" + id + "/config/validate", JSON.stringify({ importColumnMappings: mapping, "config": config }))];
                    case 1:
                        validateMappingResult = _a.sent();
                        if (!validateMappingResult.ok) return [3, 3];
                        return [4, validateMappingResult.json()];
                    case 2: return [2, _a.sent()];
                    case 3: throw "Exception saving config";
                }
            });
        });
    };
    ImportService.prototype.getErrors = function (context, id) {
        return __awaiter(this, void 0, void 0, function () {
            var errorsResult, errors;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.globalLoader.allow(true, 0);
                        return [4, this.httpClient.get(this.url + context + "/" + id + '/has-errors')];
                    case 1:
                        errorsResult = _a.sent();
                        if (!errorsResult) return [3, 3];
                        return [4, errorsResult.json()];
                    case 2:
                        errors = _a.sent();
                        return [2, errors];
                    case 3: return [2, null];
                }
            });
        });
    };
    ImportService.prototype.saveImportRowProperty = function (context, sessionId, id, propertyName, n) {
        return __awaiter(this, void 0, void 0, function () {
            var requestUri, patchDoc, patchResponse, text;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.globalLoader.allow(true, 0);
                        requestUri = this.url + context + "/" + sessionId + '/row/' + id;
                        patchDoc = {
                            propertyName: propertyName,
                            propertyValue: n
                        };
                        return [4, this.httpClient.patch(requestUri, JSON.stringify(patchDoc))];
                    case 1:
                        patchResponse = _a.sent();
                        if (!patchResponse.ok) return [3, 3];
                        return [4, patchResponse.json()];
                    case 2: return [2, _a.sent()];
                    case 3:
                        if (!(patchResponse.status == 400)) return [3, 5];
                        return [4, patchResponse.text()];
                    case 4:
                        text = _a.sent();
                        throw text;
                    case 5: return [2];
                }
            });
        });
    };
    ImportService.prototype.saveImportRowBatch = function (context, sessionId, ids, propertyName, n, saveAlias, oldValue) {
        if (saveAlias === void 0) { saveAlias = false; }
        if (oldValue === void 0) { oldValue = null; }
        return __awaiter(this, void 0, void 0, function () {
            var requestUri, patchDoc, patchResponse, text;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.globalLoader.allow(true, 0);
                        requestUri = this.url + context + "/" + sessionId + '/batch-update';
                        patchDoc = {
                            propertyName: propertyName,
                            propertyValue: n,
                            importRowIdToUpdate: ids,
                            saveAlias: saveAlias,
                            oldValue: oldValue
                        };
                        return [4, this.httpClient.patch(requestUri, JSON.stringify(patchDoc))];
                    case 1:
                        patchResponse = _a.sent();
                        if (!patchResponse.ok) return [3, 3];
                        return [4, patchResponse.json()];
                    case 2: return [2, _a.sent()];
                    case 3:
                        if (!(patchResponse.status == 400)) return [3, 5];
                        return [4, patchResponse.text()];
                    case 4:
                        text = _a.sent();
                        throw text;
                    case 5: return [2];
                }
            });
        });
    };
    ImportService.prototype.executeAction = function (context, sessionId, id, actionId) {
        return __awaiter(this, void 0, void 0, function () {
            var requestUri, patchResponse;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        requestUri = this.url + context + "/" + sessionId + '/row/' + id + '/action/' + actionId;
                        return [4, this.httpClient.post(requestUri)];
                    case 1:
                        patchResponse = _a.sent();
                        if (!patchResponse.ok) return [3, 3];
                        return [4, patchResponse.json()];
                    case 2: return [2, _a.sent()];
                    case 3:
                        if (!(patchResponse.status == 400)) return [3, 5];
                        return [4, patchResponse.text()];
                    case 4: throw _a.sent();
                    case 5: return [2];
                }
            });
        });
    };
    ImportService.prototype.getPossibleValues = function (context, sessionId, propertyName) {
        return __awaiter(this, void 0, void 0, function () {
            var requestUri, patchResponse, text;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        requestUri = this.url + context + "/" + sessionId + '/possible-values/' + propertyName;
                        return [4, this.httpClient.get(requestUri)];
                    case 1:
                        patchResponse = _a.sent();
                        if (!patchResponse.ok) return [3, 3];
                        return [4, patchResponse.json()];
                    case 2: return [2, _a.sent()];
                    case 3:
                        if (!(patchResponse.status == 400)) return [3, 5];
                        return [4, patchResponse.text()];
                    case 4:
                        text = _a.sent();
                        throw text;
                    case 5: return [2];
                }
            });
        });
    };
    ImportService.prototype.process = function (context, id) {
        return __awaiter(this, void 0, void 0, function () {
            var processResult;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.globalLoader.allow(true, 0);
                        return [4, this.httpClient.post(this.url + context + "/" + id + "/process")];
                    case 1:
                        processResult = _a.sent();
                        if (!processResult.ok) return [3, 3];
                        return [4, processResult.json()];
                    case 2: return [2, _a.sent()];
                    case 3: throw "Exception saving config";
                    case 4: return [2, null];
                }
            });
        });
    };
    ImportService = __decorate([
        aurelia_framework_1.autoinject,
        __metadata("design:paramtypes", [aurelia_fetch_client_1.HttpClient, global_loader_service_1.GlobalLoaderService])
    ], ImportService);
    return ImportService;
}());

//# sourceMappingURL=import-service.js.map
