<?php

namespace Sibs\Sibs\Checkout\Block\Checkout;

use Magento\Checkout\Block\Checkout\LayoutProcessor as MagentoLayoutProcessor;

class LayoutProcessor
{
    public function afterProcess(MagentoLayoutProcessor $subject, array $jsLayout)
    {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['street'] = [
            'component' => 'Magento_Ui/js/form/components/group',
            'label'     => __('Street Address'),
            'required'  => true,
            'dataScope' => 'shippingAddress.street',
            'provider'  => 'checkoutProvider',
            'sortOrder' => 4,
            'type'      => 'group',
            'config'    => [
                'template'          => 'ui/group/group',
                'additionalClasses' => 'street',
            ],
            'children' => [
                [
                    'component' => 'Magento_Ui/js/form/element/abstract',
                    'config'    => [
                        'customScope' => 'shippingAddress',
                        'template'    => 'ui/form/field',
                        'elementTmpl' => 'ui/form/element/input',
                    ],
                    'dataScope'         => '0',
                    'provider'          => 'checkoutProvider',
                    'validation'        => ['required-entry' => true, 'min_text_length' => 1, 'max_text_length' => 70],
                    'additionalClasses' => 'field',
                    'label'             => __('Street 1'),
                ],
                [
                    'component' => 'Magento_Ui/js/form/element/abstract',
                    'config'    => [
                        'customScope' => 'shippingAddress',
                        'template'    => 'ui/form/field',
                        'elementTmpl' => 'ui/form/element/input',
                    ],
                    'dataScope'         => '1',
                    'provider'          => 'checkoutProvider',
                    'validation'        => ['min_text_length' => 1, 'max_text_length' => 70],
                    'additionalClasses' => 'additional',
                    'label'             => __('Street 2'),
                    'visible'           => false,
                ],
                [
                    'component' => 'Magento_Ui/js/form/element/abstract',
                    'config'    => [
                        'customScope' => 'shippingAddress',
                        'template'    => 'ui/form/field',
                        'elementTmpl' => 'ui/form/element/input',
                    ],
                    'dataScope'         => '2',
                    'provider'          => 'checkoutProvider',
                    'validation'        => ['min_text_length' => 1, 'max_text_length' => 70],
                    'additionalClasses' => 'additional',
                    'label'             => __('Street 3'),
                    'visible'           => false,
                ],
                [
                    'component' => 'Magento_Ui/js/form/element/abstract',
                    'config'    => [
                        'customScope' => 'shippingAddress',
                        'template'    => 'ui/form/field',
                        'elementTmpl' => 'ui/form/element/input',
                    ],
                    'dataScope'         => '3',
                    'provider'          => 'checkoutProvider',
                    'validation'        => ['min_text_length' => 1, 'max_text_length' => 70],
                    'additionalClasses' => 'additional',
                    'label'             => __('Street 4'),
                    'visible'           => false,
                ],

            ],
        ];

        $billingConfiguration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'];

        //Checks if billing step available.
        if (isset($billingConfiguration)) {
            //Iterate over billing forms.
            foreach ($billingConfiguration as $key => &$billingForm) {
                //Exclude not billing forms
                if (! strpos($key, '-form')) {
                    continue;
                }

                $billingForm['children']['form-fields']['children'] = $this->processAddress(
                    $billingForm['children']['form-fields']['children'],
                    $billingForm['dataScopePrefix'],
                    [
                        'checkoutProvider',
                        'checkout.steps.billing-step.payment.payments-list.' . $key . '.form-fields.street',
                        'checkout.steps.billing-step.payment.payments-list.' . $key . '.form-fields.city',
                        'checkout.steps.billing-step.payment.payments-list.' . $key . '.form-fields.country_id',
                    ]
                );
            }
        }

        return $jsLayout;
    }

    private function processAddress($addressFieldset, $dataScope, $deps)
    {
        //Makes each address field label trackable.
        //remove additional street lines from payment checkout step
        if (isset($addressFieldset['street']['children'])) {
            foreach ($addressFieldset['street']['children'] as $key => $street) {
                if ($street['dataScope'] > 0) {
                    $street['visible'] = false;
                } else {
                    $street['validation']['max_text_length'] = 70;
                }

                $street['tracks']['label'] = true;
                //Remove .additional class. Can be removed, but style fix provided instead.
                $street['additionalClasses']                 = '';
                $addressFieldset['street']['children'][$key] = $street;
            }
        }

        return $addressFieldset;
    }
}
