#include "planner/expression/bound_operator_expression.hpp"

using namespace duckdb;
using namespace std;

BoundOperatorExpression::BoundOperatorExpression(ExpressionType type, TypeId return_type)
    : Expression(type, ExpressionClass::BOUND_OPERATOR, return_type) {
}

string BoundOperatorExpression::ToString() const {
	auto op = ExpressionTypeToOperator(type);
	if (!op.empty()) {
		// use the operator string to represent the operator
		if (children.size() == 1) {
			return op + "(" + children[0]->GetName() + ")";
		} else if (children.size() == 2) {
			return children[0]->GetName() + " " + op + " " + children[1]->GetName();
		}
	}
	// if there is no operator we render it as a function
	auto result = ExpressionTypeToString(type) + "(";
	for (index_t i = 0; i < children.size(); i++) {
		result += children[i]->GetName();
		if (i + 1 < children.size()) {
			result += ", ";
		} else {
			result += ")";
		}
	}
	return result;
}

bool BoundOperatorExpression::Equals(const BaseExpression *other_) const {
	if (!BaseExpression::Equals(other_)) {
		return false;
	}
	auto other = (BoundOperatorExpression *)other_;
	if (children.size() != other->children.size()) {
		return false;
	}
	for (index_t i = 0; i < children.size(); i++) {
		if (!Expression::Equals(children[i].get(), other->children[i].get())) {
			return false;
		}
	}
	return true;
}

unique_ptr<Expression> BoundOperatorExpression::Copy() {
	auto copy = make_unique<BoundOperatorExpression>(type, return_type);
	copy->CopyProperties(*this);
	for (auto &child : children) {
		copy->children.push_back(child->Copy());
	}
	return move(copy);
}
