//
// Created by sfd on 25-4-17.
//

#include "Hazel/Core/LayerStack.h"

namespace Hazel
{
    LayerStack::LayerStack()
    {
    }
    LayerStack::~LayerStack()
    {
        for (Layer* layer : m_layers)
        {
            delete layer;
        }
    }

    void LayerStack::PushLayer(Layer* layer)
    {
        m_layers.emplace(m_layers.begin() + m_LayerInsertIndex++, layer);
        m_LayerInsertIndex ++;
    }

    void LayerStack::PushOverlay(Layer* layer)
    {
        m_layers.emplace_back(layer);
    }

    void LayerStack::PopLayer(Layer* layer)
    {
        auto it = std::ranges::find(m_layers.begin(), m_layers.end(), layer);
        if (it != m_layers.end())
        {
            layer->OnDetech();
            m_layers.erase(it);
            m_LayerInsertIndex--;
        }

    }

    void LayerStack::PopOverlay(Layer* layer)
    {
        auto it = std::ranges::find(m_layers.begin(), m_layers.end(), layer);
        if (it != m_layers.end())
        {
            layer->OnDetech();
            m_layers.erase(it);
        }

    }


}