import { ActionEvent, Controller } from '@hotwired/stimulus';

import HiddenAddressInputController from '../HiddenAddressInput/HiddenAddressInput.1';

export default class extends Controller<HTMLElement> {
   static outlets = ['cart-hiddenaddressinput'];
   declare cartHiddenaddressinputOutlet: HiddenAddressInputController;

   static targets = ['select'];
   declare selectTarget: HTMLSelectElement

   async connect() {
      if (this.selectTarget.value) {
         this.cartHiddenaddressinputOutlet.changeInputValue(this.selectTarget.value);
      }
   }

   handleSelectChange(event: ActionEvent) {
      const { value } = event.target as HTMLSelectElement;

      if (value) {
         this.cartHiddenaddressinputOutlet.changeInputValue(value);
      }
   }
}
