import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ModalService } from '@libs/ui';
import { Address } from 'apps/sales/src/app/core/models/user.model';
import { undefinedToEmptyStringOrValue } from 'apps/sales/src/app/core/utils/app.utils';

@Component({
  selector: 'app-address-sugestions',
  templateUrl: './address-sugestions.component.html',
  styleUrls: ['./address-sugestions.component.scss'],
})
export class AddressSugestionsComponent implements OnInit {
  id = 'address-sugestions';
  @Input() addresses: Address[];
  @Output() select = new EventEmitter<Address>();
  @Output() ignore = new EventEmitter<boolean>();

  constructor(private modalService: ModalService) {}

  ngOnInit() {}

  openDialog() {
    this.modalService.open(this.id);
  }

  closeDialog() {
    this.modalService.close(this.id);
  }

  public addressPresentation(address: Address) {
    return `${undefinedToEmptyStringOrValue(address.street)} ${undefinedToEmptyStringOrValue(
      address.streetNo + ''
    )}, ${undefinedToEmptyStringOrValue(address.zip + '')} ${undefinedToEmptyStringOrValue(address.city)}`;
  }

  useAddress(address: Address) {
    this.select.emit(address);
    this.modalService.close(this.id);
  }

  ignoreSuggestions() {
    this.ignore.emit(true);
    this.modalService.close(this.id);
  }
}
