class Playbook::PbCurrency::Currency

def abbr_or_format_amount

def abbr_or_format_amount
  abbreviate ? abbreviated_value : formatted_amount
end

def abbreviated_value(index = 0..-2)

def abbreviated_value(index = 0..-2)
  value = amount.split(".").first.gsub(",", "").to_i
  abbreviated_num = number_to_human(value, units: { thousand: "K", million: "M", billion: "B", trillion: "T" }).gsub(/\s+/, "")
  abbreviated_num[index]
end

def absolute_amount(amount_string)

def absolute_amount(amount_string)
  amount_string.sub(/^-/, "")
end

def body_props

def body_props
  {
    text: units_element,
    color: "light",
    classname: "unit",
    dark: dark,
  }
end

def caption_props

def caption_props
  {
    text: label,
    dark: dark,
  }
end

def classname

def classname
  generate_classname("pb_currency_kit", align, size, dark_class)
end

def currency_wrapper_props

def currency_wrapper_props
  {
    classname: "dollar_sign",
    color: "light",
    dark: dark,
  }
end

def dark_class

def dark_class
  dark ? "dark" : nil
end

def decimal_value

def decimal_value
  amount.split(".")[1] || "00"
end

def emphasized_class

def emphasized_class
  emphasized ? "" : "_deemphasized"
end

def formatted_amount

def formatted_amount
  return abbreviated_value if abbreviate
  if decimals == "matching"
    if comma_separator
      number_with_delimiter(amount.gsub(",", ""))
    else
      amount
    end
  else
    whole_value
  end
end

def negative_sign

def negative_sign
  amount.starts_with?("-") && swap_negative ? "-" : ""
end

def size_value

def size_value
  case size
  when "lg"
    1
  when "md"
    3
  when "sm"
    4
  else
    3
  end
end

def swap_negative

def swap_negative
  size == "sm" && symbol != ""
end

def title_props

def title_props
  {
    size: size_value,
    text: title_text,
    classname: "pb_currency_value",
    dark: dark,
  }
end

def title_text

def title_text
  if null_display
    null_display
  elsif swap_negative
    absolute_amount(abbr_or_format_amount)
  else
    abbr_or_format_amount
  end
end

def units_element

def units_element
  return "" if decimals == "matching" && !abbreviate && !unit
  if unit.nil? && !abbreviate
    if decimals == "matching"
      ""
    else
      ".#{decimal_value}"
    end
  else
    abbreviate ? "#{abbreviated_value(-1)}#{unit}" : unit
  end
end

def variant_class

def variant_class
  case variant
  when "light"
    "_light"
  when "bold"
    "_bold"
  end
end

def whole_value

def whole_value
  value = amount.split(".").first
  if comma_separator
    number_with_delimiter(value.gsub(",", ""))
  else
    value
  end
end