"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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 };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var Entity_1 = __importDefault(require("../../Core/Entity"));
var BaseRoute = '/merchant/merchant_configuration';
var Routes = {
    RESET_DELIVERY_PIN: "".concat(BaseRoute, "/delivery_pin_reset"),
    RESET_DESTINATION_PIN: "".concat(BaseRoute, "/destination_pin_reset"),
    GET_CSV_FIELDS: "".concat(BaseRoute, "/csv_upload_fields"),
    SET_CSV_FIELDS: "".concat(BaseRoute, "/csv_upload_fields"),
    GET_USER_CSV_FIELDS: "".concat(BaseRoute, "/user_csv_upload_fields"),
    SET_USER_CSV_FIELDS: "".concat(BaseRoute, "/user_csv_upload_fields"),
    DELIVERY_PIN_CHANGE: "".concat(BaseRoute, "/delivery_pin_change"),
    RESET_EMAIL_LOGO: "".concat(BaseRoute, "/email_logo_reset"),
    UPLOAD_LOGO: "".concat(BaseRoute, "/logo"),
    RESET_CLIENT_APP_LOGO: "".concat(BaseRoute, "/client_app_logo_reset"),
    GET_RATING_REASON_CONFIGURATION: "".concat(BaseRoute, "/rating_reasons"),
    SET_RATING_REASON_CONFIGURATION: "".concat(BaseRoute, "/rating_reasons"),
    REQUEST_BETA_PROGRAM: "".concat(BaseRoute, "/request_beta_program"),
    GET_TIP_DRIVER_CONFIGURATION: "".concat(BaseRoute, "/tip_driver"),
    SET_TIP_DRIVER_CONFIGURATION: "".concat(BaseRoute, "/tip_driver"),
    GET_MOBILE_TASK_FORM: "".concat(BaseRoute, "/mobile_task_form"),
    SET_MOBILE_TASK_FORM: "".concat(BaseRoute, "/mobile_task_form"),
    UPDATE_DASHBOARD_UI_CONFIGURATION_ON_USER_LEVEL: '/dashboard_ui_configuration',
    UPDATE_DASHBOARD_UI_CONFIGURATION_ON_MERCHANT_LEVEL: '/dashboard_ui_configuration/merchant_level'
};
var MerchantConfigurationEntity = /** @class */ (function (_super) {
    __extends(MerchantConfigurationEntity, _super);
    function MerchantConfigurationEntity(session) {
        return _super.call(this, {
            session: session,
            entityName: 'merchant',
            routeSuffix: 'merchant_configuration',
            routes: []
        }) || this;
    }
    MerchantConfigurationEntity.prototype.updateMerchantConfigurationField = function (field, value) {
        var _a;
        this.service.session.update('merchantConfiguration', (_a = {}, _a[field] = value, _a));
    };
    MerchantConfigurationEntity.prototype.updateMerchantConfiguration = function (merchantConfiguration) {
        this.service.session.update('merchantConfiguration', merchantConfiguration);
    };
    MerchantConfigurationEntity.prototype.urlRequest = function (_a) {
        var route = _a.route, errorMessage = _a.errorMessage, fieldToUpdate = _a.fieldToUpdate, payload = _a.payload;
        return __awaiter(this, void 0, void 0, function () {
            var request, response;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        request = this.service.routeGenerator
                            .post(route)
                            .withExtractor(function (response) { return response.url; })
                            .setException(errorMessage);
                        if (payload) {
                            request.withPayload(payload);
                        }
                        return [4 /*yield*/, request.invoke()];
                    case 1:
                        response = _b.sent();
                        if (fieldToUpdate) {
                            this.updateMerchantConfigurationField(fieldToUpdate, response);
                        }
                        return [2 /*return*/, response];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.resetDeliveryPin = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.urlRequest({
                        route: Routes.RESET_DELIVERY_PIN,
                        fieldToUpdate: 'delivery_pin',
                        errorMessage: 'could not reset delivery pin'
                    })];
            });
        });
    };
    MerchantConfigurationEntity.prototype.resetDestinationPin = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.urlRequest({
                        route: Routes.RESET_DESTINATION_PIN,
                        fieldToUpdate: 'destination_pin',
                        errorMessage: 'could not reset destination pin'
                    })];
            });
        });
    };
    MerchantConfigurationEntity.prototype.resetEmailLogo = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.urlRequest({
                        route: Routes.RESET_EMAIL_LOGO,
                        fieldToUpdate: 'email_logo',
                        errorMessage: 'could not reset email logo'
                    })];
            });
        });
    };
    MerchantConfigurationEntity.prototype.resetClientAppLogo = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.urlRequest({
                        route: Routes.RESET_CLIENT_APP_LOGO,
                        fieldToUpdate: 'logo',
                        errorMessage: 'could not reset client app logo'
                    })];
            });
        });
    };
    MerchantConfigurationEntity.prototype.uploadLogo = function (imageFile) {
        return __awaiter(this, void 0, void 0, function () {
            var formData;
            return __generator(this, function (_a) {
                formData = new FormData();
                formData.append('file', imageFile);
                return [2 /*return*/, this.service.routeGenerator
                        .post(Routes.UPLOAD_LOGO)
                        .withHeaders({ 'Content-Type': 'multipart/form-data' })
                        .withPayload(formData)
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.get = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.service.session.merchantConfiguration) {
                            return [2 /*return*/, this.service.session.merchantConfiguration];
                        }
                        return [4 /*yield*/, this.fetchConfiguration()];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.fetchConfiguration = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.service.session;
                        return [4 /*yield*/, this.service.routeGenerator
                                .get("".concat(BaseRoute, "/"))
                                .withExtractor(function (response) { return response; })
                                .setException('could not fetch merchant_configuration')
                                .invoke()];
                    case 1:
                        _a.merchantConfiguration = _b.sent();
                        return [2 /*return*/, this.service.session.merchantConfiguration];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.update = function (diff) {
        return __awaiter(this, void 0, void 0, function () {
            var response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.routeGenerator
                            .put("".concat(BaseRoute, "/"))
                            .withExtractor(function (response) { return response.merchant_configuration; })
                            .setException('failed updating merchant_configuration')
                            .withPayload(diff)
                            .invoke()];
                    case 1:
                        response = _a.sent();
                        this.updateMerchantConfiguration(response);
                        return [2 /*return*/, response];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.getCsvFields = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .get(Routes.GET_CSV_FIELDS)
                        .withExtractor(function (response) { return response.csv_upload_fields; })
                        .setException('could not fetch csv fields configuration')
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.setCsvFields = function (csvFields) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.routeGenerator
                            .post(Routes.SET_CSV_FIELDS)
                            .setException('could not set csv fields configuration')
                            .withPayload({ csv_fields: csvFields })
                            .invoke()];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, this.getCsvFields()];
                    case 2: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.getUserCsvFields = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .get(Routes.GET_USER_CSV_FIELDS)
                        .withExtractor(function (response) { return response.csv_upload_fields; })
                        .setException('could not fetch user csv fields configuration')
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.setUserCsvFields = function (csvFields) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.routeGenerator
                            .post(Routes.SET_USER_CSV_FIELDS)
                            .setException('could not set user csv fields configuration')
                            .withPayload({ csv_fields: csvFields })
                            .invoke()];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, this.getUserCsvFields()];
                    case 2: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.deliveryPinChange = function (alternativePin) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.urlRequest({
                            route: Routes.DELIVERY_PIN_CHANGE,
                            fieldToUpdate: 'delivery_pin',
                            errorMessage: "failed updating delivery pin: ".concat(alternativePin),
                            payload: { alternate_delivery_pin: alternativePin }
                        })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.dashboardTableConfigurationUpdate = function (table_configuration) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .put(Routes.UPDATE_DASHBOARD_UI_CONFIGURATION_ON_USER_LEVEL)
                        .withPayload({ dashboard_ui_configuration: { table_configuration: table_configuration } })
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.setDashboardUiConfigurationOnMerchantLevel = function (dashboardUiConfiguration) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .put(Routes.UPDATE_DASHBOARD_UI_CONFIGURATION_ON_MERCHANT_LEVEL)
                        .withPayload({ dashboard_ui_configuration: dashboardUiConfiguration })
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.getRatingReasonConfiguration = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.routeGenerator
                            .get(Routes.GET_RATING_REASON_CONFIGURATION)
                            .withExtractor(function (response) { return response.rating_reason_config; })
                            .setException('could not fetch rating reason configuration')
                            .invoke()];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.setRatingReasonConfiguration = function (ratingReasonConfig) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.routeGenerator
                            .post(Routes.SET_RATING_REASON_CONFIGURATION)
                            .setException('could not set merchant_configuration rating reason')
                            .withPayload({ rating_reason_config: ratingReasonConfig })
                            .invoke()];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, this.getRatingReasonConfiguration()];
                    case 2: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.requestBetaProgram = function (featureName) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .post(Routes.REQUEST_BETA_PROGRAM)
                        .setException('could not set beta program')
                        .withPayload({ feature_name: featureName })
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.getTipDriverConfiguration = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .get(Routes.GET_TIP_DRIVER_CONFIGURATION)
                        .withExtractor(function (response) { return response.configuration; })
                        .setException('could not get driver tip configuration')
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.setTipDriverConfiguration = function (configuration) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.service.routeGenerator
                            .post(Routes.SET_TIP_DRIVER_CONFIGURATION)
                            .withExtractor(function (response) { return response.message; })
                            .setException('could not set driver tip configuration')
                            .withPayload({ tip_driver_configuration: configuration })
                            .invoke()];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, this.getTipDriverConfiguration()];
                    case 2: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    MerchantConfigurationEntity.prototype.getMobileTaskForm = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .get(Routes.GET_MOBILE_TASK_FORM)
                        .withExtractor(function (response) { return response.mobile_task_form; })
                        .setException('could not fetch mobile task form')
                        .invoke()];
            });
        });
    };
    MerchantConfigurationEntity.prototype.setMobileTaskForm = function (form) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this.service.routeGenerator
                        .post(Routes.SET_MOBILE_TASK_FORM)
                        .withExtractor(function (response) { return response.mobile_task_form; })
                        .setException('could not set mobile task form')
                        .withPayload({ mobile_task_form: form })
                        .invoke()];
            });
        });
    };
    return MerchantConfigurationEntity;
}(Entity_1.default));
exports.default = MerchantConfigurationEntity;
//# sourceMappingURL=MerchantConfigurationEntity.js.map