<%# locals: (name:, label:, value: nil, input_type: "text", required: false, autocomplete: nil, minlength: nil, id: nil, masked: nil) %>
<% field_id = local_assigns.fetch(:id, nil).presence || name.to_s %>
<% visible_type = local_assigns.fetch(:input_type, "text").to_s %>
<% is_email = visible_type == "email" %>
<% masked = local_assigns.key?(:masked) ? local_assigns[:masked] : !is_email %>

<div class="input-toggle<%= " input-toggle--plain" unless masked %>"<%= " data-input-toggle" if masked %>>
  <%= label_tag field_id, label %>
  <div class="input-toggle__field">
    <% if masked %>
      <%= tag.input(
            type: "password",
            name: name,
            id: field_id,
            value: local_assigns.fetch(:value, nil),
            required: local_assigns.fetch(:required, false),
            autocomplete: local_assigns.fetch(:autocomplete, nil),
            minlength: local_assigns.fetch(:minlength, nil),
            class: "input-toggle__input",
            data: { visible_type: "text" }
          ) %>
      <button type="button" class="input-toggle__btn" aria-label="Mostra <%= label.downcase %>" data-label-hide="Nascondi <%= label.downcase %>">
        <i class="fa-regular fa-eye" aria-hidden="true"></i>
      </button>
    <% elsif is_email %>
      <%= email_field_tag name, local_assigns.fetch(:value, nil),
            id: field_id,
            required: local_assigns.fetch(:required, false),
            autocomplete: local_assigns.fetch(:autocomplete, nil),
            class: "input-toggle__input input-toggle__input--plain" %>
    <% else %>
      <%= text_field_tag name, local_assigns.fetch(:value, nil),
            id: field_id,
            required: local_assigns.fetch(:required, false),
            autocomplete: local_assigns.fetch(:autocomplete, nil),
            minlength: local_assigns.fetch(:minlength, nil),
            class: "input-toggle__input input-toggle__input--plain" %>
    <% end %>
  </div>
</div>
