<commit_before>window.MultiShippingAddress ||= {}
$.extend(MultiShippingAddress, { SameAsBilling: {} })

class MultiShippingAddress.SameAsBilling.CopyBillToShip
  constructor: (@$form) ->
    @findShippingAddresses()
    @bindForm()

  billAddress: null
  shippingAddresses: []

  getName: ($input) ->
    $input.attr('name').match(/.*\[(.*)\]$/)[1]

  bindForm: ->
    @$form.on 'submit', =>
      @getBillAddress()
      @copyBillAddress()

  findShippingAddresses: ->
    shippingAddresses = @$form.find('.js-shipping-address')
    @createToggleForm(address) for address in shippingAddresses

  createToggleForm: (address) ->
    @shippingAddresses.push(new MultiShippingAddress.SameAsBilling.ToggleForm ($ address))

  copyBillAddress: ->
    @copyFormValues(address.inputs()) for address in @shippingAddresses when address.isChecked()

  copyFormValues: (addressInputs) ->
    @copyInputValue(($ input), addressInputs) for input in @billAddress

  copyInputValue: ($input, addressInputs) ->
    name = @getName $input
    addressInputs.filter("[name*=#{ name }]").val $input.val()

  getBillAddress: ->
    @billAddress =  @$form.find('#billing').find('input, select')
<commit_msg>Fix country selection bug on address checkout step<commit_after>window.MultiShippingAddress ||= {}
$.extend(MultiShippingAddress, { SameAsBilling: {} })

class MultiShippingAddress.SameAsBilling.CopyBillToShip
  constructor: (@$form) ->
    @findShippingAddresses()
    @bindForm()
    @bindCountrySelects()

  billAddress: null
  shippingAddresses: []

  getName: ($input) ->
    $input.attr('name').match(/.*\[(.*)\]$/)[1]

  bindForm: ->
    @$form.on 'submit', =>
      @getBillAddress()
      @copyBillAddress()

  bindCountrySelects: ->
    $('.js-shipping-address select.country').on 'change', (e) ->
      id = $(e.currentTarget).parent().attr('id').match(/\d+/);
      Spree.updateState "#{ id }-address"

  findShippingAddresses: ->
    shippingAddresses = @$form.find('.js-shipping-address')
    @createToggleForm(address) for address in shippingAddresses

  createToggleForm: (address) ->
    @shippingAddresses.push(new MultiShippingAddress.SameAsBilling.ToggleForm ($ address))

  copyBillAddress: ->
    @copyFormValues(address.inputs()) for address in @shippingAddresses when address.isChecked()

  copyFormValues: (addressInputs) ->
    @copyInputValue(($ input), addressInputs) for input in @billAddress

  copyInputValue: ($input, addressInputs) ->
    name = @getName $input
    addressInputs.filter("[name*=#{ name }]").val( $input.val() ).change()

  getBillAddress: ->
    @billAddress =  @$form.find('#billing').find('input, select')

{'fraction_non_alphanumeric': 0.16338880484114976, 'fraction_numerical': 0.0007564296520423601, 'mean_word_length': 4.3979591836734695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144553', 'n_tokens_mistral': 837, 'n_tokens_neox': 821, 'n_words': 147}