<%
  content_name = content.class.name

  field_id = "#{content_name}_#{field[:id]}"
  value ||= if content.respond_to?(field[:id].to_sym)
    value = content.send(field[:id].to_sym)
  else
    value = field[:value]
  end

  should_autocomplete = defined?(autocomplete) && !!autocomplete
  should_autosave     = defined?(autosave)     && !!autosave
%>

<% unless defined?(show_label) && !show_label %>
  <%= f.label field do %>
    <%= field[:label].present? ? field[:label] : ' ' %>
  <% end %>
<% end %>

<%
  placeholder = I18n.translate "attributes.#{content_name.downcase}.#{field[:label].downcase.gsub(/\s/, '_')}",
    scope: :serendipitous_questions,
    name: content.send('name') != "New #{content_name}" ? content.send('name') : "this #{content_name.downcase}",
    default: 'Write as little or as much as you want'
%>

<%= hidden_field_tag "field[name]", field[:id] %>
<%=
  text_area_tag "field[value]",
                value,
                class: "js-can-mention-pages materialize-textarea" \
                  + "#{' autocomplete js-autocomplete-' + field[:id].to_s if should_autocomplete}" \
                  + "#{' autosave-closest-form-on-change' if should_autosave}",
                placeholder: placeholder
%>

<%# todo switch to field[:options].fetch('privacy') %>
<% if field[:label].start_with?('Private') %>
  <div class="help-text show-when-focused">
    Private notes are <em>always</em> visible to only you, even if content is made public and shared.
  </div>
<% end %>

<% if defined?(autocomplete) && autocomplete && autocomplete.any? %>
  <%= content_for :javascript do %>
    $(function() {
      $('.js-autocomplete-<%= field[:id].to_s %>').autocomplete({
        limit: 5,
        data: {
          <% autocomplete.each do |autocomplete_option| %>
            "<%= autocomplete_option %>": null,
          <% end %>
        }
      });
    });
  <% end %>
<% end %>
