<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Advanced MSI for Magento 2
 */-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\InventorySourceSelectionApi\Api\GetDefaultSourceSelectionAlgorithmCodeInterface" type="Amasty\AdvancedMSI\Model\GetDefaultSourceSelectionAlgorithmCode"/>
    <preference for="Amasty\AdvancedMSI\Api\Data\ReservedProductsInterface" type="Amasty\AdvancedMSI\Model\ReservedProducts" />
    <preference for="Amasty\AdvancedMSI\Api\ReservedProductsRepositoryInterface" type="Amasty\AdvancedMSI\Model\Repository\ReservedProductsRepository" />
    <preference for="Amasty\AdvancedMSI\Api\Data\SourceCustomShippingRateInterface" type="Amasty\AdvancedMSI\Model\SourceCustomShippingRate" />
    <preference for="Amasty\AdvancedMSI\Api\SourceCustomShippingRateRepositoryInterface" type="Amasty\AdvancedMSI\Model\Repository\SourceCustomShippingRateRepository" />

    <!-- Fallback to native algorithm due virtual order processing -->
    <virtualType name="AmastyGetOriginalSourceSelectionResultFromInvoice" type="Magento\InventoryShipping\Model\GetSourceSelectionResultFromInvoice">
        <arguments>
            <argument name="getDefaultSourceSelectionAlgorithmCode" xsi:type="object">Magento\InventorySourceSelection\Model\GetDefaultSourceSelectionAlgorithmCode</argument>
        </arguments>
    </virtualType>
    <type name="Magento\InventoryShipping\Observer\VirtualSourceDeductionProcessor">
        <arguments>
            <argument name="getSourceSelectionResultFromInvoice" xsi:type="object">AmastyGetOriginalSourceSelectionResultFromInvoice</argument>
        </arguments>
    </type>
    <!-- /Fallback to native algorithm due virtual order processing -->

    <type name="Magento\InventorySourceSelection\Model\SourceSelectionService">
        <arguments>
            <argument name="sourceSelectionMethods" xsi:type="array">
                <item name="stock" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\StockBased</item>
                <item name="combined" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\Combined</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\InventorySourceSelectionApi\Model\SourceSelectionService">
        <arguments>
            <argument name="sourceSelectionMethods" xsi:type="array">
                <item name="stock" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\StockBased</item>
                <item name="combined" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\Combined</item>
            </argument>
        </arguments>
    </type>

    <type name="Magento\InventorySourceSelectionApi\Api\GetSourceSelectionAlgorithmListInterface">
        <plugin name="addAmastySourceSelectionAlgorithms" type="Amasty\AdvancedMSI\Plugin\InventorySourceSelectionApi\Model\SourceSelection\GetSourceSelectionAlgorithmListPlugin"/>
    </type>

    <virtualType name="AmastyCombinedAlgorithmDistance" type="Amasty\AdvancedMSI\Model\SourceSelection\CombinedAlgorithmData">
        <arguments>
            <argument name="class" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\Combined\DistanceBased</argument>
            <argument name="label" xsi:type="string" translate="true">Nearest Source</argument>
            <argument name="code" xsi:type="string">distance</argument>
            <argument name="defaultPriority" xsi:type="string">1</argument>
        </arguments>
    </virtualType>
    <virtualType name="AmastyCombinedAlgorithmStock" type="Amasty\AdvancedMSI\Model\SourceSelection\CombinedAlgorithmData">
        <arguments>
            <argument name="class" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\Combined\StockBased</argument>
            <argument name="label" xsi:type="string" translate="true">Available in One Source</argument>
            <argument name="code" xsi:type="const">\Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\StockBased::CODE</argument>
            <argument name="defaultPriority" xsi:type="string">2</argument>
        </arguments>
    </virtualType>
    <virtualType name="AmastyCombinedAlgorithmPriority" type="Amasty\AdvancedMSI\Model\SourceSelection\CombinedAlgorithmData">
        <arguments>
            <argument name="class" xsi:type="string">Amasty\AdvancedMSI\Model\SourceSelection\Algorithms\Combined\PriorityBased</argument>
            <argument name="label" xsi:type="string" translate="true">Source Priority (Native Algorithm)</argument>
            <argument name="code" xsi:type="string">priority</argument>
            <argument name="defaultPriority" xsi:type="string">3</argument>
            <argument name="isShouldStop" xsi:type="boolean">true</argument>
        </arguments>
    </virtualType>

    <type name="Amasty\AdvancedMSI\Model\SourceSelection\CombinedAlgorithmRegistry">
        <arguments>
            <argument name="sourceSelectionAlgorithms" xsi:type="array">
                <item name="distance" xsi:type="object">AmastyCombinedAlgorithmDistance</item>
                <item name="stock" xsi:type="object">AmastyCombinedAlgorithmStock</item>
                <item name="priority" xsi:type="object">AmastyCombinedAlgorithmPriority</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\Shipping\Model\Shipping">
        <plugin name="amastyAdvancedMSIShippingRates" sortOrder="5" type="Amasty\AdvancedMSI\Plugin\Shipping\Model\ShippingPlugin"/>
    </type>
    <type name="Magento\Shipping\Model\Shipping">
        <plugin name="Amasty_AdvancedMSI::DestinationAddress" type="Amasty\AdvancedMSI\Plugin\Shipping\Model\Shipping\DestinationAddress"/>
    </type>
    <type name="Magento\InventoryReservations\Model\AppendReservations">
        <plugin name="amastyAdvancedMSIReservationsSkip" type="Amasty\AdvancedMSI\Plugin\InventoryReservations\Model\AppendReservationsPlugin"/>
    </type>

    <type name="Magento\InventoryDistanceBasedSourceSelection\Model\Algorithms\DistanceBasedAlgorithm">
        <plugin name="amastyAdvancedMSIDistanceAlgorithmPlugin" type="Amasty\AdvancedMSI\Plugin\InventoryDistanceBasedSourceSelection\DistanceBasedAlgorithmPlugin" />
    </type>

    <type name="Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider">
        <plugin name="amastyAdvancedMSIAddCustomShippingRatesToDataProviderPlugin" type="Amasty\AdvancedMSI\Plugin\InventorySourceUi\SourceDataProvider" sortOrder="10" />
    </type>

    <type name="Magento\InventoryDistanceBasedSourceSelection\Model\DistanceProvider\GoogleMap\GetDistance">
        <plugin name="amastyAdvancedMSIAdditionalOfflineDistanceCaclulationPlugin" type="Amasty\AdvancedMSI\Plugin\InventoryDistanceBasedSourceSelection\Model\DistanceProvider\GoogleMap\GetDistancePlugin"/>
    </type>

    <type name="Magento\Framework\Setup\Declaration\Schema\Operations\CreateTable">
        <arguments>
            <argument name="triggers" xsi:type="array">
                <item name="migrateDataFromAnotherTableIfExists" xsi:type="object">Amasty\AdvancedMSI\Setup\Declaration\Schema\Db\MySQL\DDL\Triggers\MigrateDataFromAnotherTableIfExists</item>
            </argument>
        </arguments>
    </type>
    <preference for="Amasty\AdvancedMSI\Api\Data\SourceEmailInterface" type="Amasty\AdvancedMSI\Model\SourceEmail" />
    <preference for="Amasty\AdvancedMSI\Api\SourceEmailRepositoryInterface" type="Amasty\AdvancedMSI\Model\Repository\SourceEmailRepository" />

    <!-- Sample File For Import -->
    <type name="Magento\ImportExport\Model\Import\SampleFileProvider">
        <arguments>
            <argument name="samples" xsi:type="array">
                <item name="ammsi_source_import" xsi:type="string">Amasty_AdvancedMSI</item>
            </argument>
        </arguments>
    </type>
    <!-- /Sample File -->

    <type name="Amasty\AdvancedMSI\Plugin\Shipping\Model\ShippingPlugin">
        <arguments>
            <argument name="sourceSelectionItemFactory" xsi:type="object">Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionItemInterfaceFactory\Proxy</argument>
            <argument name="sourcesAssignedToStockOrderedByPriority" xsi:type="object">Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface\Proxy</argument>
            <argument name="sourceSelectionResultFactory" xsi:type="object">Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionResultInterfaceFactory\Proxy</argument>
            <argument name="sourceItemsSortedByStock" xsi:type="object">Amasty\AdvancedMSI\Model\SourceItem\GetSourceItemsSortedByStock\Proxy</argument>
        </arguments>
    </type>

    <virtualType name="AmAdvancedMSI_IsPreorder" type="Amasty\Base\Model\Di\Wrapper">
        <arguments>
            <argument name="name" xsi:type="string">Amasty\Preorder\Model\Quote\Item\IsPreorder</argument>
            <argument name="isShared" xsi:type="boolean">true</argument>
            <argument name="isProxy" xsi:type="boolean">true</argument>
        </arguments>
    </virtualType>
    <virtualType name="AmAdvancedMSI_PreorderConfigProvider" type="Amasty\Base\Model\Di\Wrapper">
        <arguments>
            <argument name="name" xsi:type="string">Amasty\Preorder\Model\ConfigProvider</argument>
            <argument name="isShared" xsi:type="boolean">true</argument>
            <argument name="isProxy" xsi:type="boolean">true</argument>
        </arguments>
    </virtualType>
    <type name="Amasty\AdvancedMSI\Plugin\Shipping\Model\ShippingPlugin">
        <arguments>
            <argument name="preorderConfigProvider" xsi:type="object">AmAdvancedMSI_PreorderConfigProvider</argument>
            <argument name="isPreorder" xsi:type="object">AmAdvancedMSI_IsPreorder</argument>
        </arguments>
    </type>
</config>
