#ifndef EMPATHY_SEMA_UNI_CONTEXT_H
#define EMPATHY_SEMA_UNI_CONTEXT_H

namespace empathy::sema
{
    class UnificationRuleSet;

    class UnificationContext
    {
        public:
            UnificationContext( const ptr< UnificationRuleSet >& rules );
            UnificationContext( ptr< UnificationRuleSet >&& rules );

            const auto& rules() const { return m_rules; }

            optional< uint32_t > getLHSHoleIndex( const StringId& name );
            optional< uint32_t > getRHSHoleIndex( const StringId& name );

            uint32_t createValue();
            void setLHSHoleIndex( const StringId& name, uint32_t index );
            void setRHSHoleIndex( const StringId& name, uint32_t index );

            const optional< Term >& getValue( uint32_t index ) const
            {
                assert( m_values.size() > index );
                return m_values[index];
            }

            UnificationContext& flip()
            {
                swap( m_currentLHSNamespaceIndex, m_currentRHSNamespaceIndex );
                return *this;
            }

        private:
            ptr< UnificationRuleSet >                   m_rules;
            immer::vector< optional< Term > >           m_values;

            uint32_t                                    m_currentLHSNamespaceIndex = 1;
            uint32_t                                    m_currentRHSNamespaceIndex = 2;

            using HoleName = pair< StringId, uint32_t >;
            ptr< unordered_map< HoleName, uint32_t > >  m_holes;

            size_t                                      m_dependencyUpdateCount = 1024;
    };
}

#endif
