#ifndef PluginScannerSubprocess_hpp
#define PluginScannerSubprocess_hpp

#include <JuceHeader.h>

class PluginScannerSubprocess final : public juce::ChildProcessWorker, private juce::AsyncUpdater
{
public:
    PluginScannerSubprocess();

    bool initialiseFromCommandLine (const juce::String& commandLine,
                                    const juce::String& commandLineUniqueID,
                                    int timeoutMs = 0);

    void handleMessageFromCoordinator (const juce::MemoryBlock& mb) override;
    void handleConnectionLost() override;
    void handleAsyncUpdate() override;
    
private:
    juce::OwnedArray<juce::PluginDescription> doScan (const juce::MemoryBlock& block);
    void sendResults (const juce::OwnedArray<juce::PluginDescription>& results);
    
    std::mutex mutex;
    std::queue<juce::MemoryBlock> pendingBlocks;

    // After construction, this will only be accessed by doScan so there's no need
    // to worry about synchronisation.
    juce::AudioPluginFormatManager formatManager;
};

#endif /* PluginScannerSubprocess_hpp */
