/*
 * Copyright (c) 2020, Rauli Laine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <snek/interpreter.hpp>
#include <snek/type/list.hpp>
#include <snek/value/list.hpp>

namespace snek::value
{
  List::List(const container_type& elements)
    : m_elements(elements) {}

  std::shared_ptr<type::Base>
  List::type(const Interpreter& interpreter) const
  {
    type::Ptr common_type;
    bool already_tried = false;

    // TODO: Implement proper type reification.
    for (const auto& element : m_elements)
    {
      const auto element_type = element->type(interpreter);

      if (common_type)
      {
        if (!common_type->matches(element_type))
        {
          common_type.reset();
          already_tried = true;
        }
      }
      else if (!already_tried)
      {
        common_type = element_type;
      }
    }

    return std::make_shared<type::List>(
      common_type ? common_type : interpreter.any_type()
    );
  }

  bool
  List::equals(const Ptr& that) const
  {
    const auto size = m_elements.size();
    ListPtr that_list;

    if (that->kind() != Kind::List)
    {
      return false;
    }
    that_list = std::static_pointer_cast<List>(that);
    if (size != that_list->m_elements.size())
    {
      return false;
    }
    for (std::size_t i = 0; i < size; ++i)
    {
      if (!m_elements[i]->equals(that_list->m_elements[i]))
      {
        return false;
      }
    }

    return true;
  }

  std::u32string
  List::to_string() const
  {
    bool first = true;
    std::u32string result;

    result += U"[";
    for (const auto& element : m_elements)
    {
      if (first)
      {
        first = false;
      } else {
        result += U", ";
      }
      result += element->to_string();
    }
    result += U"]";

    return result;
  }
}
