import { ActionEvent, Controller } from '@hotwired/stimulus';
import { Component, getComponent } from '@symfony/ux-live-component';

export default class extends Controller<HTMLDivElement> {
   private addressListComponent: Component;
   private addressComponent: Component;

   async initialize() {
      this.addressListComponent = await getComponent(document.querySelector(COMPONENTS['Account:UserAddressesList'].selector));
      this.addressComponent = await getComponent(this.element);

      const edit = this.addressComponent.getData('edit');
      const id = this.addressComponent.getData('id');

      if (edit === false && id === null){
         void this.addressComponent.action('toggleEdit')
      }
   }

   async handleDelete(event: ActionEvent) {
      if (!window.confirm(event.params.message)) {
         return;
      }

      void this.addressListComponent.action('deleteAddress', {id: event.params.id});
   }
}
