/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

define([
  'underscore',
  'ko',
  'mageUtils',
  'Magento_Checkout/js/view/shipping-address/list', // original component extended by afd
  'uiLayout',
  'Magento_Customer/js/model/address-list'
], function (_, ko, utils, Component, layout, addressList) {
  'use strict'

  var defaultRendererTemplate = {
    parent: '${ $.$data.parentName }',
    name: '${ $.$data.name }',
    // component: 'Magento_Checkout/js/view/shipping-address/address-renderer/default'
    component: 'Afd_Pce/js/view/shipping-address/address-renderer/default'
  }

  return Component.extend({
    defaults: {
      template: 'Magento_Checkout/shipping-address/list',
      visible: addressList().length > 0,
      rendererTemplates: []
    },

    /** @inheritdoc */
    initialize: function () {
      this._super()
      return this
    },

    /**
     * Create new component that will render given address in the address list
     *
     * @param {Object} address
     * @param {*} index
     */
    createRendererComponent: function (address, index) {
      var rendererTemplate, templateData, rendererComponent

      if (index in this.rendererComponents) {
        this.rendererComponents[index].address(address)
      } else {
        // rendererTemplates are provided via layout
        rendererTemplate =
          address.getType() != undefined &&
          this.rendererTemplates[address.getType()] != undefined
            ? utils.extend(
                {},
                defaultRendererTemplate,
                this.rendererTemplates[address.getType()]
              )
            : defaultRendererTemplate
        templateData = {
          parentName: this.name,
          name: index
        }
        rendererComponent = utils.template(rendererTemplate, templateData)
        utils.extend(rendererComponent, {
          address: ko.observable(address)
        })
        layout([rendererComponent])
        this.rendererComponents[index] = rendererComponent
      }
    }
  })
})
