<?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\CustomerGroupPrices\Api\Data\CustomerGroupPricesInterface" type="MageWorx\CustomerGroupPrices\Model\CustomerGroupPrices" />
    <preference for="MageWorx\CustomerGroupPrices\Api\ExportHandlerInterface" type="MageWorx\CustomerGroupPrices\Model\ImportExport\ExportHandler" />
    <preference for="MageWorx\CustomerGroupPrices\Api\ImportHandlerInterface" type="MageWorx\CustomerGroupPrices\Model\ImportExport\ImportHandler" />
    <preference for="MageWorx\CustomerGroupPrices\Api\ProductGroupPriceStorageInterface" type="MageWorx\CustomerGroupPrices\Model\Product\Price\ProductGroupPriceStorage" />
    <preference for="MageWorx\CustomerGroupPrices\Api\ProductGroupSpecialPriceStorageInterface" type="MageWorx\CustomerGroupPrices\Model\Product\Price\ProductGroupSpecialPriceStorage" />
    <preference for="MageWorx\CustomerGroupPrices\Api\Data\CustomerGroupPricesInterface" type="MageWorx\CustomerGroupPrices\Model\CustomerGroupPrices" />
    <preference for="MageWorx\CustomerGroupPrices\Api\Data\PriceUpdateResultInterface" type="MageWorx\CustomerGroupPrices\Model\Product\Price\PriceUpdateResult" />
    <preference for="MageWorx\CustomerGroupPrices\Api\CustomerGroupPriceStorageInterface" type="MageWorx\CustomerGroupPrices\Model\CustomerGroup\Price\CustomerGroupPriceStorage" />

    <type name="MageWorx\CustomerGroupPrices\Model\Product\Price\Validation\ProductGroupPriceValidator">
        <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">bundle</item>
                <item name="3" xsi:type="string">downloadable</item>
            </argument>
        </arguments>
    </type>

    <!-- REINDEX Product Price Magento <= 2.2.5 -->
    <type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice">
        <plugin name="mageworx_customergroupprices_apply_group_prices_after_reindex_default_price"
                type="MageWorx\CustomerGroupPrices\Plugin\Indexer\MagentoTill225\AddCustomerGroupPricesToProductPriceIndexPlugin"/>
    </type>

    <!-- REINDEX Product Price Magento >= 2.2.6 -->
    <type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Query\BaseFinalPrice">
        <plugin name="mageworx_customergroupprices_apply_group_prices_after_reindex_product_price"
                type="MageWorx\CustomerGroupPrices\Plugin\Indexer\MagentoFrom226\FinalPricePlugin"/>
    </type>

    <type name="Magento\Bundle\Model\ResourceModel\Indexer\Price">
        <plugin name="mageworx_customergroupprices_apply_group_prices_after_reindex_bundle_product_price"
                type="MageWorx\CustomerGroupPrices\Plugin\Indexer\MagentoFrom226\BundlePricePlugin"/>
    </type>

    <!-- Join attribute process -->
    <type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Query\JoinAttributeProcessor">
        <plugin name="mageworx_customergroupprices_join_attribute_processor"
                type="MageWorx\CustomerGroupPrices\Plugin\Indexer\MagentoFrom226\AttributeProcessPlugin"/>
    </type>

    <!-- REINDEX Catalog Rule-->
    <type name="Magento\CatalogRule\Model\Indexer\IndexBuilder">
        <plugin name="mageworx_customergroupprices_apply_group_prices_before_catalog_rule"
                type="MageWorx\CustomerGroupPrices\Plugin\AddCustomerGroupPricesToCatalogRuleIndexPlugin"/>
    </type>

    <!-- REINDEX Catalog Rule-->
    <type name="Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder">
        <plugin name="mageworx_customergroupprices_apply_group_prices_before_catalog_rule_product_select_builder"
                type="MageWorx\CustomerGroupPrices\Plugin\AddCustomerGroupPricesToRuleProductsPlugin"/>
    </type>

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

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

    <!--
    Replaces group price to base price.
    Plugin used on product, cart, checkout and search 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_customergroupprices_apply_group_prices_to_base_price"
                type="MageWorx\CustomerGroupPrices\Plugin\AddGroupPricesToBasePricePlugin" sortOrder="10" disabled="false"/>
    </type>
</config>
