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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { Component, Input } from '@angular/core';
import { ComponentHelper } from '../../../classes/component-helper';
import { CheckoutService } from '../../../services/checkout/checkout.service';
import { ShipmentService } from '../../../services/checkout/shipment.service';
import { ProductsService } from '../../../services/products/products.service';
import { ToastService } from '../../../utils/toast.service';
var ShipmentEcComponent = /** @class */ (function (_super) {
    __extends(ShipmentEcComponent, _super);
    function ShipmentEcComponent(shipmentService, toastrService, productsService, checkoutService) {
        var _this = _super.call(this) || this;
        _this.shipmentService = shipmentService;
        _this.toastrService = toastrService;
        _this.productsService = productsService;
        _this.checkoutService = checkoutService;
        _this.loading_internal = false;
        _this.points = [];
        _this.costos = null;
        _this.costs = [];
        _this.contracts = [];
        _this.emitResult = function (method_name, contract_data) {
            contract_data = __assign(__assign({}, contract_data), { shipping_method_contract: method_name + '_' + contract_data.name, shipping_method_branch_code: method_name == "branch_delivery" ? contract_data.sucursal_id : contract_data.name });
            _this.shipmentService.setContract(contract_data);
        };
        _this.verifyValidate = function (contract, data) {
            try {
                _this.costs.forEach(function (cost) { return cost.contracts.forEach(function (co) { return co.selected = false; }); });
                _this.costs.find(function (cost) { return cost.name == contract; }).contracts.find(function (contra) { return contra.name == data.name; }).selected = true;
            }
            catch (error) {
                console.log("error al buscar contrato", error);
            }
            _this.emitResult(contract, data);
        };
        _this.toTraducible = function (name) { return name.replace(/_/g, '-'); };
        _this.deleteUPS = function (methods) { return methods.filter(function (method) { return method.name != 'UPS'; }); };
        _this.setMethod = function (method) {
            _this.loading_internal = true;
            _this.shipmentService.restartStep();
            setTimeout(function () {
                _this.shipmentService.setMethod(method).then(function (res) { return _this.loading_internal = false; });
            }, 1000);
        };
        _this.validName = function (name) { return isNaN(name); };
        _this.showResponse = function (costs) { return console.log(costs); };
        _this.ecOnConstruct();
        return _this;
    }
    ShipmentEcComponent.prototype.ngOnInit = function () {
        var _this = this;
        this.shipmentService.initialize(this.step_id);
        this.shipmentService.getShipmentMethods();
        this.checkoutService.asociatedData$.subscribe(function (data) {
            _this.costos = data && data.find(function (value) { return value.type == "shipment"; }) || null;
        });
        this.shipmentService.costs.subscribe(function (cos) {
            var _a, _b;
            _this.costs = cos.map(function (cost) { return __assign(__assign({}, cost), { contracts: cost.contracts.map(function (co) { return __assign(__assign({}, co), { selected: false }); }) }); });
            if (_this.costs.length && ((_a = _this.costs[0]) === null || _a === void 0 ? void 0 : _a.name) && ((_b = _this.costs[0]) === null || _b === void 0 ? void 0 : _b.contracts[0])) {
                _this.verifyValidate(_this.costs[0].name, _this.costs[0].contracts[0]);
            }
        });
        this.ecOnInit();
    };
    ShipmentEcComponent.prototype.ngOnChanges = function () {
        this.shipmentService.getShipmentMethods();
    };
    ShipmentEcComponent.prototype.setActive = function (e) {
        var e_1, _a;
        var parent = e.target.parentElement.children;
        try {
            for (var parent_1 = __values(parent), parent_1_1 = parent_1.next(); !parent_1_1.done; parent_1_1 = parent_1.next()) {
                var button = parent_1_1.value;
                if (button.classList.contains('active')) {
                    button.classList.remove('active');
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (parent_1_1 && !parent_1_1.done && (_a = parent_1.return)) _a.call(parent_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        e.target.classList.add("active");
    };
    ShipmentEcComponent.ctorParameters = function () { return [
        { type: ShipmentService },
        { type: ToastService },
        { type: ProductsService },
        { type: CheckoutService }
    ]; };
    __decorate([
        Input()
    ], ShipmentEcComponent.prototype, "step_id", void 0);
    ShipmentEcComponent = __decorate([
        Component({
            selector: 'app-shipment-ec',
            template: "<!-- <div class=\"checkout-title\">\n    <h3>{{'shipment-methods'|translate}}</h3>\n</div> -->\n\n<div class=\"container-fluid\">\n    <div class=\"col-12\">\n        <div class=\"card text-center\">\n            <div class=\"card-header text-dark\">\n                <h4>{{ 'select-method' | translate }}</h4>\n                <div class=\"btn-toolbar justify-content-center\" role=\"toolbar\" aria-label=\"Envios toolbar\"\n                    *ngIf=\"(shipmentService.methods | async) as methods; else noMethods\">\n                    <div class=\"btn-group group-shipping\" role=\"group\" aria-label=\"Grupo botones envio\">\n                        <div class=\"d-flex align-content-start flex-wrap btn-group btn-group-toggle\">\n                            <button type=\"button\" class=\"btn btn-outline-secondary\"\n                                *ngFor=\"let method of deleteUPS(methods); let x = index\"\n                                (click)=\"setMethod(method.code, post_code); setActive($event)\">\n                                {{ method.name | translate }}\n                            </button>\n                        </div>\n\n\n                    </div>\n                </div>\n            </div>\n            <div class=\"card-body\">\n                <div class=\"row justify-content-center\" *ngIf=\"this.costs\">\n                    \n                    <ng-container *ngIf=\"!loading_internal ; else loading\">\n                        <div class=\"col-auto text-dark\" *ngFor=\"let cost of costs\">\n                            <!-- {{showResponse(cost)}} -->\n                            <ng-container *ngIf=\"cost.contracts.length > 0\">\n                                <span><b>{{ toTraducible(cost.name) | translate }}</b></span>\n                                <hr />\n                                <div class=\"option\" *ngFor=\"let contract of cost.contracts; let i = index\">\n                            \n                                    <!-- *ngIf=\"cost.name == 'home_delivery' \" -->\n                                    <div class=\"form-check\">\n                                        <input class=\"form-check-input\" [checked]=\"contract.selected\" type=\"radio\" name=\"card\" [id]=\"cost.name + i\"\n                                            value=\"dark\" (click)=\"verifyValidate(cost.name, contract)\">\n                                        <label class=\"form-check-label\" [for]=\"cost.name + i\" aria-label=\"Dark grey\">\n                                            <span\n                                                [class]=\"(cost.name == 'home_delivery') ? 'shipment-contractname home_delivery' : 'shipment-contractname' \"\n                                                *ngIf=\"validName(contract.name)\">{{ contract.name | translate }}</span>\n                                            <span *ngIf=\"contract.computed\"><br class=\"shipment-contractname\">{{ contract.computed | translate }}<br\n                                                    class=\"shipment-contractname\"></span>\n                                            <div *ngIf=\"contract.detail\">\n                                                <div *ngFor=\"let line of contract.detail.split(' - '); let i = index\">\n                                                    <span *ngIf=\"i == 0\"><b>{{ line }}</b></span>\n                                                    <span class=\"inside-detail\" *ngIf=\"i > 0\">{{ line }}</span>\n                                                </div>\n                                            </div>\n                                            <span *ngIf=\"contract.price > 0\" [class]=\"'inside-detail ' + ( costos?.amount == 0 ? ' free ' : '') \">\n                                                <br class=\"shipment-contractname\" *ngIf=\"!contract.computed\">\n                                                {{ ('price'|translate) + ': ' + (contract.price | ecCurrencySymbol) }}</span>\n                                            <span *ngIf=\"contract.price == 0\" [class]=\"'inside-detail ' + ( costos?.amount == 0 ? ' free ' : '') \">\n                                                <br class=\"shipment-contractname\" *ngIf=\"!contract.computed\"> {{ ('free'|translate) }}</span>\n                                            <br>\n                                        </label>\n                                    </div>\n                                </div>\n                            </ng-container>\n                        </div>\n                    </ng-container>\n                </div>\n            </div>\n\n        </div>\n    </div>\n\n</div>\n\n<ng-template #noMethods>\n    <div class=\"d-flex flex-row w-100 justify-content-center mt-2\">\n        <h5 class=\"text-secondary\">{{'no-shipment-methods'|translate}}</h5>\n    </div>\n</ng-template>\n\n<ng-template #loading>\n    <app-loading-full-ec></app-loading-full-ec>\n</ng-template>",
            providers: [ShipmentService],
            styles: [".shipment-container{width:100%;max-height:500px!important}.points-container{margin-top:10px;max-height:300px;overflow:scroll}agm-map{height:300px}.ancho-100{width:200px}.ancho-50{width:100px}.group-shipping{margin:10px 0}.option-container>div{margin-bottom:15px}@media only screen and (min-width:600px){.option-container{display:flex;justify-content:space-between;margin-top:20px}}hr{margin-top:0!important}.option{display:flex;align-items:flex-start;margin-bottom:20px}.option label{margin:0 0 0 10px}.inside-detail{color:gray!important;font-weight:400}.inside-detail.free{text-decoration:line-through}.active{color:#fff!important}"]
        })
    ], ShipmentEcComponent);
    return ShipmentEcComponent;
}(ComponentHelper));
export { ShipmentEcComponent };
//# sourceMappingURL=data:application/json;base64, 