{% from '@bonniernews/dn-design-system-web/components/icon-sprite/icon-sprite.njk' import IconUse %}
{% from '@bonniernews/dn-design-system-web/assets/form-field/form-field.njk' import FormField %}
{% from '@bonniernews/dn-design-system-web/njk-helpers/attributes.njk' import getAttributes %}

{# Internal helper macro - not intended for use outside of this file #}
{% macro CheckboxIcons(componentClassName) %}
  <span class="{{ componentClassName + '__icon'}}">
    {{ IconUse({ icon: "check_box_outline_blank" }) }}
    {{ IconUse({ icon: "check_box-filled" }) }}
  </span>
{% endmacro %}

{% macro Checkbox(params) %}
  {% set componentClassName = "ds-checkbox" %}
  {% set classNamePrefix = componentClassName + "--" %}
  {% set name = params.name | default("ds-checkbox") %}
  {% set attributes = getAttributes(params.attributes) %}

  {%- set wrapperParams = { error: params.error, errorMessage: params.errorMessage if params.label else false } %}

  {%- set wrapperClasses = [
    componentClassName,
    "ds-force-px" if params.forcePx,
    params.classNames if params.classNames
  ] %}

  {% call FormField(wrapperParams, wrapperClasses) %}
    <input type="checkbox" id="{{ name }}" name="{{ name }}" class="{{ componentClassName + '__input' }} js-field-error"
      data-validation="{{ params.validation }}" {{- attributes | safe }}
      {{- " checked" if params.checked }} {{-" disabled" if params.disabled }} {{-" required" if params.required }}  />
    {% if params.stripLabel %}
      <div class="{{ componentClassName + '__inner'}}">
        {{ CheckboxIcons(componentClassName) }}
      </div>
    {% else %}
      <label class="{{ componentClassName + '__inner'}}" for="{{ name }}">
        {{ CheckboxIcons(componentClassName) }}
        {% if params.label %}
          <span class="{{ componentClassName + '__text'}}">{{ params.label }}</span>
        {% endif %}
      </label>
    {% endif %}
  {% endcall %}
{% endmacro %}
