{#
/**
 * @file
 * Default theme implementation to display a paragraph.
 *
 * Available variables:
 * - paragraph: Full paragraph entity.
 *   Only method names starting with "get", "has", or "is" and a few common
 *   methods such as "id", "label", and "bundle" are available. For example:
 *   - paragraph.getCreatedTime() will return the paragraph creation timestamp.
 *   - paragraph.id(): The paragraph ID.
 *   - paragraph.bundle(): The type of the paragraph, for example, "image" or "text".
 *   - paragraph.getOwnerId(): The user ID of the paragraph author.
 *   See Drupal\paragraphs\Entity\Paragraph for a full list of public properties
 *   and methods for the paragraph object.
 * - content: All paragraph items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class element may contain one or more of the following
 *   classes:
 *   - paragraphs: The current template type (also known as a "theming hook").
 *   - paragraphs--type-[type]: The current paragraphs type. For example, if the paragraph is an
 *     "Image" it would result in "paragraphs--type--image". Note that the machine
 *     name will often be in a short form of the human readable label.
 *   - paragraphs--view-mode--[view_mode]: The View Mode of the paragraph; for example, a
 *     preview would result in: "paragraphs--view-mode--preview", and
 *     default: "paragraphs--view-mode--default".
 * - view_mode: View mode; for example, "preview" or "full".
 * - logged_in: Flag for authenticated user status. Will be true when the
 *   current user is a logged-in member.
 * - is_admin: Flag for admin user status. Will be true when the current user
 *   is an administrator.
 *
 * @see template_preprocess_paragraph()
 *
 * @ingroup themeable
 */
#}
{{  attach_library('kamihaya_cms_paragraph_type/background-image-css') }}

{% set v_margin = paragraph.field_vertical_margin.0.value %}
{% set paragraph_id = paragraph.id() %}

{%
  set classes = [
    'paragraph',
    paragraph_id ? 'paragraph-id--' ~ paragraph_id,
    'paragraph--type--' ~ paragraph.bundle|clean_class,
    view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
    not paragraph.isPublished() ? 'paragraph--unpublished',
    v_margin ? v_margin,
  ]
%}
{% block paragraph %}
  {% block content %}
    <div{{attributes.addClass(classes)}}>
      {% set imageUrl = content.field_background_image_url %}
      {% set width = paragraph.field_width.0.value %}
      {% set width_unit = paragraph.field_width_unit.0.value %}
      {% set align = content.field_align|render|striptags|trim|lower %}
      {% set position = content.field_background_position|render|striptags|trim|lower %}
      {% set repeat = content.field_background_repeat|render|striptags|trim|lower %}
      {% set size = content.field_background_size|render|striptags|trim|lower %}
      {# Component ID #}
      {% if content.field_component_id|render %}
        {% set component_id = content.field_component_id|render|striptags|trim %}
      {% endif %}
      {# CSS Class #}
      {% if content.field_css_class|render %}
        {% set css_class = content.field_css_class|render|striptags|trim %}
      {% endif %}
      {%
        set style = [
          width ? (width_unit == 'px' ? ('width: 100%; max-width: ' ~ width ~ width_unit ~ ';') : ('width: ' ~ width ~ (width_unit ? width_unit ~ ';' : 'px;'))) : 'width: 100%;',
          imageUrl ? 'background-image: url(' ~ imageUrl ~ ');',
          size ? 'background-size: ' ~ size ~ ';',
          position ? 'background-position: ' ~ position ~ ';',
          'background-repeat: ' ~ (repeat == 1? 'repeat;' : 'no-repeat;'),
        ]
      %}
      {% if width_unit == 'vw' %}
        {%
          set style = style|merge([
            'margin-left: calc(-50vw + 50%);',
            'max-width: none;',
          ])
        %}
      {% endif %}
      {% if content.field_background_mobile_image|render %}
        {% set mobileImageUrl = content.field_background_mobile_image_url %}
        <style>
          @media (max-width: 767px) {
            .paragraph-id--{{ paragraph_id }} .bg-image {
              background-image: url('{{ mobileImageUrl }}') !important;
            }
          }
        </style>
      {% endif %}
      {% set bg_attribute = create_attribute() %}
      {%
        set bg_classes = [
          'bg-image',
          align ? 'align-' ~ align,
          css_class ? css_class,
        ]
      %}

      <div{{bg_attribute.addClass(bg_classes).setAttribute('style', style|join(' '))}} {% if component_id %} id="{{ component_id }}"{% endif %}>
        <div class="bg-image-item">
          {{ content.field_body }}
        </div>
      </div>
    </div>
  {% endblock %}
{% endblock paragraph %}
