class WcoEmail::EmailFilter

#
# 2023-03-04 vp When I receive one.
#

def to_s

def to_s
  "EmailFilter: #{from_regex} #{from_exact} #{conditions.map { |c| c.to_s }.join }"
end

def to_s_full

def to_s_full
  # inn = ""
  # inn = "#{inn}#{conditions.map { |c| c.to_s_full }.join }" if conditions.present?
  # inn = "#{inn}#{skip_conditions.map { |c| c.to_s_full }.join }" if skip_conditions.present?
  out =<<-AOL
mailFilter #{from_regex} #{from_exact}>
conditions.map { |c| c.to_s_full( indent: 2) }.join }
skip_conditions.map { |c| c.to_s_full( indent: 2) }.join }
actions.map { |c| c.to_s_full( indent: 2) }.join }
EmailFilter>"
L
  while out.match(/\n\n/) do
    out = out.gsub(/\n\n/, "\n")
  end
  out
end

def to_xml

def to_xml
  attrs = ''
  children = ''
  if from_regex || from_exact
    attrs = "#{attrs} from=#{from_regex}#{from_exact}"
  end
  if conditions.present?
    children = "#{children}#{conditions.map { |c| c.to_s }.join('') }"
  end
  return "<EF #{attrs}>#{children}</EF>\n"
end

def validate_actions

validate :validate_actions
def validate_actions
  if actions.length == 0
    errors.add(:actions, 'must be present')
  end
end

def validate_conditions

validate :validate_conditions
def validate_conditions
  if conditions.length + skip_conditions.length == 0
    errors.add(:conditions, 'Either conditions or skip conditions must be present.')
  end
end