#include "__header.h"
#include "Exception.h"
#include "Operand.h"
#include <iostream>

namespace jet {
   
   __header::__header(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) {
      output = false;
      if(!global.cgi)
	throw coreutils::Exception("must enable cgi mode on jet tag to use header tag.");
      if(!keywordDefined("name"))
	throw coreutils::Exception("header tag must have name defined.");
      if(!keywordDefined("expr") && keywordDefined("value") && hasContainer)
	throw coreutils::Exception("header tag cannot have both value and a container.");
      if(keywordDefined("expr") && !keywordDefined("value") && hasContainer)
	throw coreutils::Exception("header tag cannot have both expr and a container.");
      if(keywordDefined("expr") && keywordDefined("value") && !hasContainer)
	throw coreutils::Exception("header tag cannot have both expr and value.");
      if(!keywordDefined("expr") && !keywordDefined("value") && !hasContainer)
	throw coreutils::Exception("header tag must have a value, expr or a container.");
      if(keywordDefined("expr")) {
	 if(keywordDefined("eval"))
	   throw coreutils::Exception("Cannot use eval with expr.");	 
	 global.headers[resolveKeyword("name")] = Operand(keywords["expr"], *this).string;	    
      } else if(hasContainer) {
	 processContainer(container);
	 if(evaluate) {
	    global.headers[resolveKeyword("name")] = out;	       
	 } else {	   
	    global.headers[resolveKeyword("name")] = container;
	 }	    
      } else 
	global.headers[resolveKeyword("name")] = resolveKeyword("value");
     
   }      
}
