{#
================================================================================
SVG Icons
================================================================================

Add new icons by dropping a svg into the 'src/icons' folder.

Usage:

{% import 'macros' as macros %}
{{ macros.icon("outline/iconFilename") }}

#}

{% macro icon(iconFilename, classes, attributes) %}
	{% import _self as macros %}
	<span class="icon {{ "icon-#{iconFilename|replace('/', '-')}" }}{{ classes ? ' ' ~ classes }}"{{ macros.addAttributes(attributes) }}>
		<svg><use xlink:href="{{ "##{iconFilename}" }}"/></svg>
	</span>
{% endmacro %}

{#
================================================================================
Eager Load
================================================================================

Craft's Eager-Loading Elements allows you to nicely optimize your templates by
telling Craft to load everything you need in one big query.

Usage:

{% import 'macros' as macros %}
{{ macros.eager(entry, ['bannerImage']) }}

#}
{% macro eager(element, fields) %}
	{% set element = element|default %}
	{% set fields = fields|default([]) %}

	{% if element and fields|length %}

		{% do craft.app.elements.eagerLoadElements(
			className(element),
			[element],
			fields
		) %}

	{% endif %}

{% endmacro %}

{#
================================================================================
Add Attributes
================================================================================

Converts an object to a list of attributes. Great for keeping things clean when
working with a bunch of values. If value is falsy then it won't output the
attribute.

Usage:
{% import '_macros' as macros %}
{% set attributes = {
	class: 'link'
	href: '#',
	target: link.blank ? "_blank",
} %}
<a {{ macros.addAttributes(attributes) }}>Text</a>

#}

{% macro addAttributes(attributes) %}
	{% set output %}
		{% for key, value in attributes if key and value is not null %} {% spaceless %}
			{{ key }}="{{ value|default }}"
		{% endspaceless %}{% endfor %}
	{% endset %}
	{{ output | trim | raw }}
{% endmacro %}


{#
================================================================================
Filesize Macro
================================================================================

Converts byes to human-readable format.

Usage:

{% import 'macros' as macros %}
{{ macros.bytesToSize(bytes) }}

#}

{% macro bytesToSize(bytes) %}{% spaceless %}
	{% set kilobyte = 1024 %}
	{% set megabyte = kilobyte * 1024 %}
	{% set gigabyte = megabyte * 1024 %}
	{% set terabyte = gigabyte * 1024 %}

	{% if bytes < kilobyte %}
		{{ bytes ~ 'B' }}
	{% elseif bytes < megabyte %}
		{{ (bytes / kilobyte)|number_format(0, '.') ~ 'KB' }}
	{% elseif bytes < gigabyte %}
		{{ (bytes / megabyte)|number_format(1, '.') ~ 'MB' }}
	{% elseif bytes < terabyte %}
		{{ (bytes / gigabyte)|number_format(2, '.') ~ 'GB' }}
	{% else %}
		{{ (bytes / terabyte)|number_format(2, '.') ~ 'TB' }}
	{% endif %}
{% endspaceless %}{% endmacro %}


{#
================================================================================
Http prefix checker
================================================================================

This makes sure a url has a http(s) prefix and helps avoid broken links.

#}

{% macro parseUrl(url, prefix) -%}
{{- url starts with 'http://' or url starts with 'https://'
	? url
	: (prefix ? prefix ~ url : 'http://' ~ url)
-}}
{%- endmacro %}


{#
================================================================================
Format a full address
================================================================================

#}

{% macro formatAddress(addressLine1, addressLine2, suburb, state, postcode) -%}{%- spaceless %}
	{% set addressesSuburb = [addressLine1,addressLine2,suburb]|join(', ') %}
	{% set fulladdress = (addressesSuburb ~ ' ' ~ (state|upper) ~ ' ' ~ postcode)|trim|replace({' , ':' '}) %}
	{# This removes empty addresses - a little nasty but works #}
	{{ fulladdress|replace({',':''})|trim|length == 0 ? '' : fulladdress }}
{% endspaceless -%}
{%- endmacro %}
