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;
};
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';
let ShipmentEcComponent = class ShipmentEcComponent extends ComponentHelper {
    constructor(shipmentService, toastrService, productsService, checkoutService) {
        super();
        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 = (method_name, contract_data) => {
            contract_data = Object.assign(Object.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 = (contract, data) => {
            try {
                this.costs.forEach(cost => cost.contracts.forEach(co => co.selected = false));
                this.costs.find(cost => cost.name == contract).contracts.find(contra => contra.name == data.name).selected = true;
            }
            catch (error) {
                console.log("error al buscar contrato", error);
            }
            this.emitResult(contract, data);
        };
        this.toTraducible = (name) => name.replace(/_/g, '-');
        this.deleteUPS = (methods) => methods.filter(method => method.name != 'UPS');
        this.setMethod = (method) => {
            this.loading_internal = true;
            this.shipmentService.restartStep();
            setTimeout(() => {
                this.shipmentService.setMethod(method).then(res => this.loading_internal = false);
            }, 1000);
        };
        this.validName = (name) => isNaN(name);
        this.showResponse = (costs) => console.log(costs);
        this.ecOnConstruct();
    }
    ngOnInit() {
        this.shipmentService.initialize(this.step_id);
        this.shipmentService.getShipmentMethods();
        this.checkoutService.asociatedData$.subscribe(data => {
            this.costos = data && data.find(value => value.type == "shipment") || null;
        });
        this.shipmentService.costs.subscribe(cos => {
            var _a, _b;
            this.costs = cos.map(cost => { return Object.assign(Object.assign({}, cost), { contracts: cost.contracts.map(co => { return Object.assign(Object.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();
    }
    ngOnChanges() {
        this.shipmentService.getShipmentMethods();
    }
    setActive(e) {
        const parent = e.target.parentElement.children;
        for (const button of parent) {
            if (button.classList.contains('active')) {
                button.classList.remove('active');
            }
        }
        e.target.classList.add("active");
    }
};
ShipmentEcComponent.ctorParameters = () => [
    { 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);
export { ShipmentEcComponent };
//# sourceMappingURL=data:application/json;base64, 