<?xml version="1.0"?>
<!--
/**
 * Copyright © MageWorx. All rights reserved.
 * See LICENSE.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="MageWorx\CustomerPrices\Api\Data\CustomerPricesInterface" type="MageWorx\CustomerPrices\Model\CustomerPrices" />
    <preference for="MageWorx\CustomerPrices\Api\CustomerPricesRepositoryInterface" type="MageWorx\CustomerPrices\Model\CustomerPricesRepository" />
    <preference for="MageWorx\CustomerPrices\Api\Data\PriceUpdateResultInterface" type="MageWorx\CustomerPrices\Model\Customer\Price\PriceUpdateResult" />
    <preference for="MageWorx\CustomerPrices\Api\CustomerPriceStorageInterface" type="MageWorx\CustomerPrices\Model\Customer\Price\CustomerPriceStorage" />
    <preference for="MageWorx\CustomerPrices\Api\CustomerProductPriceStorageInterface" type="MageWorx\CustomerPrices\Model\Product\Price\CustomerProductPriceStorage" />

    <type name="MageWorx\CustomerPrices\Model\ProductTypeProvider">
        <arguments>
            <argument name="allowedProductTypes" xsi:type="array">
                <item name="0" xsi:type="string">simple</item>
                <item name="1" xsi:type="string">virtual</item>
                <item name="2" xsi:type="string">downloadable</item>
            </argument>
        </arguments>
    </type>

    <virtualType name="CustomerPricesFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
        <arguments>
            <argument name="appliers" xsi:type="array">
                <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
                <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
            </argument>
        </arguments>
    </virtualType>

    <virtualType name="CustomerPricesDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">MageWorx\CustomerPrices\Model\ResourceModel\CustomerPrices\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">CustomerPricesFilterPool</argument>
        </arguments>
    </virtualType>
    <type name="MageWorx\CustomerPrices\Model\ResourceModel\CustomerPrices\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">mageworx_customerprices</argument>
            <argument name="resourceModel" xsi:type="string">MageWorx\CustomerPrices\Model\ResourceModel\CustomerPrices</argument>
        </arguments>
    </type>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="customerprices_listing_data_source" xsi:type="string">MageWorx\CustomerPrices\Model\ResourceModel\CustomerPrices\Collection</item>
            </argument>
        </arguments>
    </type>

    <!-- REINDEX Catalog Rule-->
    <type name="Magento\CatalogRule\Model\Indexer\IndexBuilder">
        <plugin name="apply_customer_prices_before_catalog_rule"
                type="MageWorx\CustomerPrices\Plugin\AddCustomerPricesToCatalogRuleIndexPlugin"/>
    </type>

    <!-- change create temp table search_tmp_********  for Magento <= 2.1.7 -->
    <type name="Magento\CatalogSearch\Model\Search\TableMapper">
        <plugin name="apply_customer_prices_search_table_mapper"
                type="MageWorx\CustomerPrices\Plugin\Search\ApplyCustomerPricesToTableMapper"/>
    </type>

    <!-- change create temp table search_tmp_********  for Magento >= 2.1.8 -->
    <type name="Magento\CatalogSearch\Model\Search\FilterMapper\ExclusionStrategy">
        <plugin name="apply_customer_prices_search_filter_mapper"
                type="MageWorx\CustomerPrices\Plugin\Search\ApplyCustomerPricesToFilterMapper"/>
    </type>

    <!-- Change Identifier Cache -->
    <type name="Magento\Framework\App\Http\Context">
        <plugin name="mageworx_customer_prices_cache"
                type="MageWorx\CustomerPrices\Plugin\Cache\ChangeCacheIdentifierPlugin"
                sortOrder="10"
                disabled="false"/>
    </type>

    <!-- Change widget Identifier Cache -->
    <type name="Magento\Framework\View\Element\AbstractBlock">
        <plugin name="mageworx_customer_prices_cache_widget"
                type="MageWorx\CustomerPrices\Plugin\Cache\ChangeWidgetCacheIdentifierPlugin"
                sortOrder="10"
                disabled="false"/>
    </type>

    <!-- Add customer price to catalog rule -->
    <type name="Magento\CatalogRule\Model\ResourceModel\Rule">
        <plugin name="apply_customer_prices_catalog_rule"
                type="MageWorx\CustomerPrices\Plugin\CatalogRulePlugin"/>
    </type>

    <type name="Magento\Framework\Console\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="mageworx_customer_prices_synchronize" xsi:type="object">MageWorx\CustomerPrices\Console\Command\SynchronizeCommand</item>
            </argument>
        </arguments>
    </type>

    <!--! Return id child of configurable product with min price -->
    <type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\LinkedProductSelectBuilderByIndexPrice">
        <plugin name="mageworx_customer_prices_get_child_entity_id"
                type="MageWorx\CustomerPrices\Plugin\GetEntityIdChildOfConfigurableProductWithMinPrice"/>
    </type>

    <!--
    Replaces base price to customer price.
    Plugin used on product, cart and checkout pages in Magento Commerce Edition.
    Plugin used on cart and checkout pages in Magento Open Source Edition.
    -->
    <type name="Magento\Catalog\Model\Product\Type\Price">
        <plugin name="mageworx_customer_prices_add_customer_prices_to_base_price_price"
                type="MageWorx\CustomerPrices\Plugin\AddCustomerPricesToBasePricePlugin"/>
    </type>

    <preference for="Magento\Theme\Block\Html\Title" type="MageWorx\CustomerPrices\Block\Theme\Html\Title"/>

</config>
