class Playbook::PbDocs::KitExample

def description

def description
  @description ||= read_kit_file("docs", "_#{example_key}.md")
end

def example

def example
  if type == "rails"
    render inline: source
  elsif type == "react"
    react_component example_key.camelize, { dark: dark }
  elsif type == "swift"
    ## render the markdown file
    render inline: source
  end
end

def highlighter

def highlighter
  type.eql?("rails") ? "erb" : "react"
end

def read_kit_file(folder, *args)

def read_kit_file(folder, *args)
  path = ::Playbook.kit_path(kit, folder, *args)
  path.exist? ? path.read : ""
end

def sanitize_code(stringified_code)

def sanitize_code(stringified_code)
  stringified_code = stringified_code.gsub('"../.."', '"playbook-ui"')
                                     .gsub('"../../"', '"playbook-ui"')
                                     .gsub("'../../'", "'playbook-ui'")
                                     .gsub("'../..'", "'playbook-ui'")
                                     .gsub(%r{from "../.*}, "from 'playbook-ui'")
                                     .gsub(%r{from '../.*}, "from 'playbook-ui'")
  stringified_code = dark ? stringified_code.gsub("{...props}", "dark") : stringified_code.gsub(/\s*{...props}\s*\n/, "\n")
  if stringified_code.include?("props: { ")
    stringified_code = stringified_code.gsub("props: {", "props: {dark: true,") if type == "rails" && dark
  elsif type == "rails" && dark
    stringified_code = stringified_code.gsub("props: {", "props: {\n    dark: true,")
  end
  stringified_code.gsub(" {...props}", "")
end

def source

def source
  @source ||= begin
    extension = if type == "rails"
                  "html.erb"
                else
                  type == "swift" ? "swift" : "jsx"
                end
    stringified_code = read_kit_file("docs", "_#{example_key}.#{extension}")
    sanitize_code(stringified_code)
  end
end

def swift_source

def swift_source
  read_kit_file("", "_#{example_key}.swift")
end

def tsx_source

def tsx_source
  read_kit_file("", "_#{example_key}.tsx")
end