<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Customer Attributes Base for Magento 2
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Amasty\CustomerAttributes\Api\RelationRepositoryInterface" type="Amasty\CustomerAttributes\Model\RelationRepository"/>
    <preference for="Amasty\CustomerAttributes\Api\Data\RelationInterface" type="Amasty\CustomerAttributes\Model\Relation"/>
    <preference for="Amasty\CustomerAttributes\Api\Data\RelationDetailInterface" type="Amasty\CustomerAttributes\Model\RelationDetails"/>
    <preference for="selectimg" type="Magento\Framework\Data\Form\Element\Select"/>
    <preference for="multiselectimg" type="Magento\Framework\Data\Form\Element\Multiselect"/>
    <preference for="Amasty\CustomerAttributes\Api\Data\CustomerAttributesQuoteAddressInterface" type="Amasty\CustomerAttributes\Model\Quote\Address\CustomerAttributesQuoteAddress"/>
    <preference for="Amasty\CustomerAttributes\Api\CustomerAttributesQuoteAddressRepositoryInterface" type="Amasty\CustomerAttributes\Model\CustomerAttributesQuoteAddressRepository"/>
    <preference for="Amasty\CustomerAttributes\Api\OptionDataRepositoryInterface" type="Amasty\CustomerAttributes\Model\Eav\Option\OptionDataRepository"/>
    <preference for="Amasty\CustomerAttributes\Api\Data\OptionDataInterface" type="Amasty\CustomerAttributes\Model\Eav\Option\OptionData"/>

    <preference for="Magento\Customer\Model\Metadata\Form\File" type="Amasty\CustomerAttributes\Model\Customer\Metadata\Form\File"/>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="amcustomerattr_relation_grid_data_source" xsi:type="string">Amasty\CustomerAttributes\Model\ResourceModel\Relation\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <type name="\Magento\Quote\Model\Quote\Address\CustomAttributeList">
        <plugin name="Amasty_CustomerAttributes::CustomAttributeList" type="Amasty\CustomerAttributes\Plugin\Quote\Address\CustomAttributeList" />
    </type>
    <type name="\Magento\CustomerCustomAttributes\Model\Quote\Address\CustomAttributeList">
        <plugin name="Amasty_CustomerAttributes::CustomAttributeListEnterprise" type="Amasty\CustomerAttributes\Plugin\Quote\Address\CustomAttributeList" />
    </type>
    <type name="\Magento\Quote\Model\Quote">
        <plugin name="Amasty_CustomerAttributes::Quote" type="Amasty\CustomerAttributes\Plugin\Quote\Quote" />
    </type>
    <type name="Magento\Customer\Model\Metadata\Form">
        <plugin name="Amasty_CustomerAttributes::metadata-form" type="Amasty\CustomerAttributes\Plugin\Customer\Metadata\Form" />
    </type>
    <type name="Magento\Eav\Model\Validator\Attribute\Data">
        <plugin name="Amasty_CustomerAttributes::customer-arrtibute-validation" type="Amasty\CustomerAttributes\Plugin\Eav\Model\Validator\Data"/>
    </type>
    <type name="Magento\Framework\Webapi\ServiceInputProcessor">
        <plugin name="Amasty_CustomerAttributes::convert_multiple_values-to-array"
                type="Amasty\CustomerAttributes\Plugin\Webapi\ServiceInputProcessor"/>
    </type>


    <!-- product save plugin. SAve customer group if attribute with this type exist-->
    <type name="Magento\Customer\Api\CustomerRepositoryInterface">
        <plugin name="Amasty_CustomerAttributes::customer-group" type="Amasty\CustomerAttributes\Plugin\Customer\Api\CustomerRepositoryInterface" />
    </type>
    <type name="Magento\Rule\Model\Condition\AbstractCondition">
        <plugin name="Amasty_CustomerAttributes::add-condition-types" type="Amasty\CustomerAttributes\Plugin\Rule\Condition" />
    </type>

    <!-- for fixing fatal Call to undefined method Magento\Customer\Model\Indexer\Source::addAttributeToSelect() -->
    <type name="Magento\Framework\Indexer\Handler\AttributeHandler">
        <plugin name="Amasty_CustomerAttributes::customer-arrtibute-validation" type="Amasty\CustomerAttributes\Plugin\Framework\Indexer\Handler\AttributeHandler"/>
    </type>

    <type name="Magento\Ui\Config\Reader\Definition\Data">
        <plugin name="Amasty_CustomerAttributes::getDefenition" type="Amasty\CustomerAttributes\Plugin\Ui\Config\Reader\Definition\Data" />
    </type>

    <type name="Magento\Customer\Model\FileProcessor">
        <plugin name="Amasty_CustomerAttributes::saveTemporaryFile" type="Amasty\CustomerAttributes\Plugin\Customer\Model\FileProcessor" />
    </type>
    <type name="Magento\Customer\Model\FileUploader">
        <plugin name="Amasty_CustomerAttributes::uploadFile" type="Amasty\CustomerAttributes\Plugin\Customer\Model\FileUploader" />
    </type>
    <type name="Magento\Eav\Model\Entity\Attribute\Source\Table">
        <plugin name="Amasty_CustomerAttributes::addIconOptions" type="Amasty\CustomerAttributes\Plugin\Eav\Model\Entity\Attribute\Source\Table" />
    </type>


    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\Document">
        <plugin name="Amasty_CustomerAttributes::addIconOptions" type="Amasty\CustomerAttributes\Plugin\View\Element\UiComponent\DataProvider\Document" />
    </type>
    <type name="Magento\Customer\Model\Metadata\Form\AbstractData">
        <plugin name="Amasty_CustomerAttributes::extractData" type="Amasty\CustomerAttributes\Plugin\Customer\Model\Metadata\Form\AbstractData" />
    </type>

    <type name="Magento\Email\Model\Template">
        <plugin name="Amasty_CustomerAttributes::customer_email_attributes" type="Amasty\CustomerAttributes\Plugin\Email\Model\Template" />
    </type>

    <type name="Amasty\PDFCustom\Model\Template">
        <!-- Customer attribute variables for PDF Customizer-->
        <plugin name="Amasty_CustomerAttributes::customer_PDF_attributes" type="Amasty\CustomerAttributes\Plugin\Email\Model\Template" />
    </type>
    <type name="Amasty\CustomerAttributes\Model\ResourceModel\Relation\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">amasty_customer_attributes_relation</argument>
            <argument name="resourceModel" xsi:type="string">\Amasty\CustomerAttributes\Model\ResourceModel\Relation</argument>
        </arguments>
    </type>

    <!-- Double plugins fix for arrays in custom attributes in address -->
    <!-- Delete Plugin After fix Magento Bug with customer Custom Attributes bug with new logic  \Magento\Customer\Model\Address\AbstractAddress LINE 284 -->
    <type name="\Magento\Customer\Model\Address\AbstractAddress">
        <plugin name="Amasty_CustomerAttributes::fix_magento_bug" type="Amasty\CustomerAttributes\Plugin\Customer\Model\Address\AbstractAddressPlugin"/>
    </type>

    <type name="Magento\CustomerCustomAttributes\Model\CustomerAddressCustomAttributesProcessor">
        <plugin name="Amasty_CustomerAttributes::multiselect_attributes_fix" type="Amasty\CustomerAttributes\Plugin\CustomerCustomAttributes\Model\MultiselectAttributesFix" />
    </type>

    <!-- /Double plugins fix for arrays in custom attributes in address -->

    <!-- Delete Plugin After fix Magento Bug with type of custom attribute - file(attachment), bug with new logic  \Magento\Customer\Model\Customer\Attribute\Validator\File LINE 41 -->
    <type name="Magento\Customer\Model\Customer\Attribute\Validator\File">
        <plugin name="Amasty_CustomerAttributes::file_validator" type="Amasty\CustomerAttributes\Plugin\Customer\Model\Customer\Attribute\Validator\FilePlugin" sortOrder="0"/>
    </type>

    <type name="Amasty\CommonRules\Model\Modifiers\Address">
        <plugin name="Amasty_CustomerAttributes::modify_address"
                type="Amasty\CustomerAttributes\Plugin\CommonRules\Model\Modifiers\Address\ModifyAddress"/>
    </type>

    <type name="Amasty\Base\Plugin\Setup\Model\DeclarationInstaller\ApplyPatchesBeforeDeclarativeSchema">
        <arguments>
            <argument name="moduleNames" xsi:type="array">
                <item name="customerAttributes" xsi:type="string">
                    Amasty_CustomerAttributes
                </item>
            </argument>
        </arguments>
    </type>
</config>
