<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\ResourceModel\Product">
        <plugin name="algoliaProducts" type="Algolia\AlgoliaSearch\Model\Indexer\ProductObserver"/>
    </type>
    <type name="Magento\Catalog\Model\Product\Action">
        <plugin name="algoliaProductsMassAction" type="Algolia\AlgoliaSearch\Model\Indexer\ProductObserver"/>
    </type>
    <type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">
        <plugin name="algoliaStockItems" type="Algolia\AlgoliaSearch\Plugin\StockItemObserver" disabled="true"/>
    </type>

    <type name="Magento\Catalog\Model\ResourceModel\Category">
        <plugin name="algoliaCategories" type="Algolia\AlgoliaSearch\Model\Indexer\CategoryObserver"/>
    </type>

    <type name="Magento\Catalog\Model\Category">
        <plugin name="algolia_algoliasearch_plugin_category_url" type="Algolia\AlgoliaSearch\Plugin\CategoryUrlPlugin"/>
    </type>

    <type name="Magento\Cms\Model\ResourceModel\Page">
        <plugin name="algoliaPages" type="Algolia\AlgoliaSearch\Model\Indexer\PageObserver"/>
    </type>

    <type name="Magento\Search\Helper\Data">
        <plugin name="algoliaSearchHelperData" type="Algolia\AlgoliaSearch\Plugin\SearchHelperDataPlugin"/>
    </type>

    <!-- Dependency Injection for new Model -->
    <preference for="Algolia\AlgoliaSearch\Api\Data\JobInterface" type="Algolia\AlgoliaSearch\Model\Job" />
    <preference for="Algolia\AlgoliaSearch\Api\Data\LandingPageInterface" type="Algolia\AlgoliaSearch\Model\LandingPage" />
    <preference for="Algolia\AlgoliaSearch\Api\Data\QueryInterface" type="Algolia\AlgoliaSearch\Model\Query" />
    <preference for="Algolia\AlgoliaSearch\Api\RecommendManagementInterface" type="Algolia\AlgoliaSearch\Model\RecommendManagement" />

    <!-- Declare preference for QueueArchiveRepositoryInterface -->
    <preference for="Algolia\AlgoliaSearch\Api\QueueArchiveRepositoryInterface" type="Algolia\AlgoliaSearch\Model\QueueArchiveRepository" />

    <!-- Declare implementation for QueueArchiveInterface -->
    <preference for="Algolia\AlgoliaSearch\Api\Data\QueueArchiveInterface" type="Algolia\AlgoliaSearch\Model\QueueArchive" />

    <preference for="Algolia\AlgoliaSearch\Api\Insights\EventProcessorInterface" type="Algolia\AlgoliaSearch\Service\Insights\EventProcessor"/>

    <preference for="Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface" type="Algolia\AlgoliaSearch\Service\Product\ReplicaManager"/>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="algolia_algoliasearch_job_listing_data_source" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\Job\Grid\Collection</item>
                <item name="algolia_algoliasearch_log_listing_data_source" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\Run\Grid\Collection</item>
                <item name="algolia_algoliasearch_landingpage_listing_data_source" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\LandingPage\Grid\Collection</item>
                <item name="algolia_algoliasearch_query_listing_data_source" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\Query\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Model\ResourceModel\Job\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">algoliasearch_queue</argument>
            <argument name="eventPrefix" xsi:type="string">algoliasearch_queue_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">algoliasearch_queue_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\Job</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Model\ResourceModel\Run\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">algoliasearch_queue_log</argument>
            <argument name="eventPrefix" xsi:type="string">algoliasearch_log_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">algoliasearch_log_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\Run</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Model\ResourceModel\LandingPage\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">algoliasearch_landing_page</argument>
            <argument name="eventPrefix" xsi:type="string">algoliasearch_landing_page_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">algoliasearch_landing_page_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\LandingPage</argument>
        </arguments>
    </type>

    <type name="Algolia\AlgoliaSearch\Model\ResourceModel\Query\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">algoliasearch_query</argument>
            <argument name="eventPrefix" xsi:type="string">algoliasearch_query_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">algoliasearch_query_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\Query</argument>
        </arguments>
    </type>

    <type name="Magento\Framework\EntityManager\MetadataPool">
        <arguments>
            <argument name="metadata" xsi:type="array">
                <item name="Algolia\AlgoliaSearch\Api\Data\LandingPageInterface" xsi:type="array">
                    <item name="entityTableName" xsi:type="string">algoliasearch_landing_page</item>
                    <item name="identifierField" xsi:type="string">landing_page_id</item>
                </item>
                <item name="Algolia\AlgoliaSearch\Api\Data\QueryInterface" xsi:type="array">
                    <item name="entityTableName" xsi:type="string">algoliasearch_query</item>
                    <item name="identifierField" xsi:type="string">query_id</item>
                </item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\Framework\EntityManager\HydratorPool">
        <arguments>
            <argument name="hydrators" xsi:type="array">
                <item name="Algolia\AlgoliaSearch\Api\Data\LandingPageInterface" xsi:type="string">Magento\Framework\EntityManager\AbstractModelHydrator</item>
                <item name="Algolia\AlgoliaSearch\Api\Data\QueryInterface" xsi:type="string">Magento\Framework\EntityManager\AbstractModelHydrator</item>
            </argument>
        </arguments>
    </type>
    <virtualType name="Algolia\AlgoliaSearch\QueryImageUpload" type="Algolia\AlgoliaSearch\Model\ImageUploader">
        <arguments>
            <argument name="baseTmpPath" xsi:type="string">algolia_img/tmp</argument>
            <argument name="basePath" xsi:type="string">algolia_img</argument>
            <argument name="allowedExtensions" xsi:type="array">
                <item name="jpg" xsi:type="string">jpg</item>
                <item name="jpeg" xsi:type="string">jpeg</item>
                <item name="gif" xsi:type="string">gif</item>
                <item name="png" xsi:type="string">png</item>
            </argument>
        </arguments>
    </virtualType>
    <type name="Algolia\AlgoliaSearch\Controller\Adminhtml\Query\Imageupload">
        <arguments>
            <argument name="imageUploader" xsi:type="object">Algolia\AlgoliaSearch\QueryImageUpload</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Controller\Adminhtml\Query\Save">
        <arguments>
            <argument name="imageUploader" xsi:type="object">Algolia\AlgoliaSearch\QueryImageUpload</argument>
        </arguments>
    </type>

    <!-- Click and Conversion Analytics -->
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="algoliasearch_query_quote_item_conversion" type="Algolia\AlgoliaSearch\Plugin\QuoteItem"/>
    </type>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="algolia_algoliasearch_queuearchive_listing_data_source" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\QueueArchive\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <virtualType name="Algolia\AlgoliaSearch\Model\ResourceModel\QueueArchive\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">algoliasearch_queue_archive</argument>
            <argument name="resourceModel" xsi:type="string">Algolia\AlgoliaSearch\Model\ResourceModel\QueueArchive\Collection</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\Console\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="replica_sync_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand</item>
                <item name="replica_delete_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\ReplicaDeleteCommand</item>
                <item name="replica_rebuild_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand</item>
                <item name="replica_disable_virtual_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\ReplicaDisableVirtualCommand</item>
                <item name="synonym_deduplicate_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\SynonymDeduplicateCommand</item>
                <!-- custom indexers -->
                <item name="reindex_products_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\IndexProductsCommand</item>
                <item name="reindex_categories_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\IndexCategoriesCommand</item>
                <item name="reindex_pages_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\IndexPagesCommand</item>
                <item name="reindex_suggestions_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\IndexSuggestionsCommand</item>
                <item name="reindex_additional_sections_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\IndexAdditionalSectionsCommand</item>
                <item name="delete_products_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\DeleteProductsCommand</item>
                <item name="process_queue_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\ProcessQueueCommand</item>
                <item name="reindex_all_command" xsi:type="object">Algolia\AlgoliaSearch\Console\Command\Indexer\IndexAllCommand</item>
            </argument>
        </arguments>
    </type>

    <type name="Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand">
        <arguments>
            <argument name="replicaManager" xsi:type="object">Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface\Proxy</argument>
            <argument name="productHelper" xsi:type="object">Algolia\AlgoliaSearch\Helper\Entity\ProductHelper\Proxy</argument>
        </arguments>
    </type>

    <type name="Algolia\AlgoliaSearch\Console\Command\ReplicaDeleteCommand">
        <arguments>
            <argument name="replicaManager" xsi:type="object">Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface\Proxy</argument>
        </arguments>
    </type>

    <type name="Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand">
        <arguments>
            <argument name="replicaManager" xsi:type="object">Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface\Proxy</argument>
            <argument name="productHelper" xsi:type="object">Algolia\AlgoliaSearch\Helper\Entity\ProductHelper\Proxy</argument>
        </arguments>
    </type>

    <type name="Algolia\AlgoliaSearch\Console\Command\ReplicaDisableVirtualCommand">
        <arguments>
            <argument name="configHelper" xsi:type="object">Algolia\AlgoliaSearch\Helper\ConfigHelper\Proxy</argument>
            <argument name="replicaManager" xsi:type="object">Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface\Proxy</argument>
            <argument name="productHelper" xsi:type="object">Algolia\AlgoliaSearch\Helper\Entity\ProductHelper\Proxy</argument>
        </arguments>
    </type>

    <type name="Algolia\AlgoliaSearch\Console\Command\SynonymDeduplicateCommand">
        <arguments>
            <argument name="algoliaConnector" xsi:type="object">Algolia\AlgoliaSearch\Service\AlgoliaConnector\Proxy</argument>
            <argument name="indexOptionsBuilder" xsi:type="object">Algolia\AlgoliaSearch\Service\Product\IndexOptionsBuilder\Proxy</argument>
        </arguments>
    </type>

    <!-- custom indexers -->
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\IndexProductsCommand">
        <arguments>
            <argument name="productBatchQueueProcessor" xsi:type="object">Algolia\AlgoliaSearch\Service\Product\BatchQueueProcessor\Proxy</argument>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\IndexCategoriesCommand">
        <arguments>
            <argument name="categoryBatchQueueProcessor" xsi:type="object">Algolia\AlgoliaSearch\Service\Category\BatchQueueProcessor\Proxy</argument>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\IndexPagesCommand">
        <arguments>
            <argument name="pageBatchQueueProcessor" xsi:type="object">Algolia\AlgoliaSearch\Service\Page\BatchQueueProcessor\Proxy</argument>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\IndexSuggestionsCommand">
        <arguments>
            <argument name="suggestionBatchQueueProcessor" xsi:type="object">Algolia\AlgoliaSearch\Service\Suggestion\BatchQueueProcessor\Proxy</argument>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\IndexAdditionalSectionsCommand">
        <arguments>
            <argument name="additionalSectionBatchQueueProcessorr" xsi:type="object">Algolia\AlgoliaSearch\Service\AdditionalSection\BatchQueueProcessor\Proxy</argument>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\DeleteProductsCommand">
        <arguments>
            <argument name="productBatchQueueProcessor" xsi:type="object">Algolia\AlgoliaSearch\Service\Product\BatchQueueProcessor\Proxy</argument>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\ProcessQueueCommand">
        <arguments>
            <argument name="configHelper" xsi:type="object">Algolia\AlgoliaSearch\Helper\ConfigHelper\Proxy</argument>
            <argument name="queue" xsi:type="object">Algolia\AlgoliaSearch\Model\Queue\Proxy</argument>
            <argument name="algoliaCredentialsManager" xsi:type="object">Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager\Proxy</argument>
        </arguments>
    </type>
    <type name="Algolia\AlgoliaSearch\Console\Command\Indexer\IndexAllCommand">
        <arguments>
            <argument name="storeNameFetcher" xsi:type="object">Algolia\AlgoliaSearch\Service\StoreNameFetcher\Proxy</argument>
        </arguments>
    </type>
    <!-- custom indexers END -->

    <type name="Algolia\AlgoliaSearch\Logger\AlgoliaLogger">
        <arguments>
            <argument name="name" xsi:type="string">algolia</argument>
            <argument name="handlers" xsi:type="array">
                <item name="system" xsi:type="object">Algolia\AlgoliaSearch\Logger\Handler\SystemLoggerHandler</item>
                <item name="custom" xsi:type="object">Algolia\AlgoliaSearch\Logger\Handler\AlgoliaLoggerHandler</item>
            </argument>
        </arguments>
    </type>
    <preference for="Algolia\AlgoliaSearch\Api\LoggerInterface" type="Algolia\AlgoliaSearch\Logger\AlgoliaLogger"/>

    <!-- url model management for indexing -->
    <virtualType name="Algolia\AlgoliaSearch\Factory\Product\FrontendUrlFactory" type="Magento\Framework\UrlFactory">
        <arguments>
            <argument name="instanceName" xsi:type="string">Magento\Framework\Url</argument>
        </arguments>
    </virtualType>
    <virtualType name="Algolia\AlgoliaSearch\Model\Product\FrontendProductUrl" type="Magento\Catalog\Model\Product\Url">
        <arguments>
            <argument name="urlFactory" xsi:type="object">Algolia\AlgoliaSearch\Factory\Product\FrontendUrlFactory</argument>
        </arguments>
    </virtualType>
    <type name="Algolia\AlgoliaSearch\Service\Product\RecordBuilder">
        <arguments>
            <argument name="productUrl" xsi:type="object">Algolia\AlgoliaSearch\Model\Product\FrontendProductUrl</argument>
        </arguments>
    </type>
</config>
