"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var handleResponses = function (res) {
    if (typeof res === "object" && "state" in res) {
        if (res.state !== "SUCCESS") {
            throw new Error("Received an invalid response state from the gfx service \"" + res.state + "\"");
        }
    }
    return res;
};
var Service = /** @class */ (function () {
    function Service(options) {
        this.options = options;
        this.requester = new ig_utilities_1.Http.Service({
            baseUrl: options.basePath,
            defaults: {
                json: true,
            },
            middlewares: [
                function (url, options, next) {
                    return next(url, options).then(handleResponses);
                },
            ],
        });
    }
    Service.prototype.getProducts = function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var res;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.requester.post("Get/Products", {
                            Scope: this.options.scope,
                            Password: this.options.password,
                        })];
                    case 1:
                        res = _a.sent();
                        return [2 /*return*/, res.map(function (product) { return ({
                                articleId: "nativeId" in product ? product.nativeId : product.NativeId,
                                product: "product" in product ? product.product : product.Product,
                            }); })];
                }
            });
        });
    };
    Service.prototype.configure = function (articleId, configuration, previousHashes) {
        return this.configureWithScope(tslib_1.__assign({ articleId: articleId,
            configuration: configuration, hashes: previousHashes }, this.createScopeBody()));
    };
    Service.prototype.configureWithScope = function (_a) {
        var articleId = _a.articleId, configuration = _a.configuration, previousHashes = _a.hashes, scope = _a.scope, password = _a.password;
        var hashes = previousHashes === undefined
            ? {}
            : {
                VisualHash: previousHashes.visual,
                GeometricHash: previousHashes.geometric,
                PbrHash: previousHashes.pbr,
            };
        return this.requester.post("Configure", tslib_1.__assign(tslib_1.__assign({ Password: password, Scope: scope }, hashes), { Product: articleId, State: configuration }));
    };
    //#endregion
    Service.prototype.createScopeBody = function () {
        if (ig_utilities_1.isNullOrUndefined(this.options.scope) ||
            ig_utilities_1.isNullOrUndefined(this.options.password)) {
            throw new Error("This operation requires a scope and password to be provided");
        }
        return {
            scope: this.options.scope,
            password: this.options.password,
        };
    };
    return Service;
}());
exports.Service = Service;
