@import './utilities/map';
@import './utilities/strip-unit';
@import './utilities/highlight-helper';

// =============================================================================
// Deprecation errors
// =============================================================================

@if variable-exists(default-horizontal-gutters) {
    @error ('$default-horizontal-gutters is no longer in use. Please rename to $default-horizontal-base-unit.');
}

@if variable-exists(default-vertical-gutters) {
    @error ('$default-vertical-gutters is no longer in use. Please rename to $default-vertical-base-unit.');
}

// =============================================================================
// Grid
// =============================================================================

/*
 * Shorthand convenience functions for grid-container and grid-column.
 */

@mixin grid($args...) {
    @include grid-container($args...);
}

@mixin column($args...) {
    @include grid-column($args...);
}

/*
 * TODO Docs
 */
@mixin grid-container(
    $nested: false,
    $use-horizontal-gutters: $default-use-horizontal-gutters,
    $use-vertical-gutters: $default-use-vertical-gutters,
    $margin-top: $default-container-margin-top,
    $margin-bottom: $default-container-margin-bottom,
    $padding-top: $default-container-padding-top,
    $padding-bottom: $default-container-padding-bottom,
    $column-padding-top: $default-column-padding-top,
    $column-padding-bottom: $default-column-padding-bottom,
    $column-margin-top: $default-column-margin-top,
    $column-margin-bottom: $default-column-margin-bottom,
    $padding-right: $default-container-padding-right,
    $padding-left: $default-container-padding-left
) {
    position: relative;

    display: flex;
    flex-direction: row;
    flex-wrap: wrap;

    align-items: stretch;

    // Columns have *almost* complete autonomy from their parent grid container, with a very important exception being vertical spacing.
    // This is to ensure that all columns within a container are evenly spaced along the vertical axis, with the top-most columns'
    // top-margins negated by the negative margin of the grid container. Otherwise, headaches happen.

    @if $use-vertical-gutters {
        $margin-top: $margin-top - $column-margin-top;

        @include grid-container-vertical-metrics(
            $nested: $nested,
            $margin-top: $margin-top,
            $margin-bottom: $margin-bottom,
            $padding-top: $padding-top,
            $padding-bottom: $padding-bottom
        );

        & > * {
            @include grid-column-vertical-metrics(
                $padding-top:
                    if(
                        $column-padding-top != null,
                        $column-padding-top,
                        $default-column-padding-top
                    ),
                $padding-bottom:
                    if(
                        $column-padding-bottom != null,
                        $column-padding-bottom,
                        $default-column-padding-bottom
                    ),
                $margin-top: $column-margin-top,
                $margin-bottom: $column-margin-bottom
            );
        }
    }

    // Horizontal padding and margins are set here.
    // For top-most grid containers, the left and right *padding* are set from $default-container-padding-left and $default-container-padding-right.
    // For nested grid containers, the left and right *margins* are set from $default-horizontal-base-unit.
    @include grid-container-horizontal-metrics(
        $nested: $nested,
        $use-horizontal-gutters: $use-horizontal-gutters,
        $padding-left: $padding-left,
        $padding-right: $padding-right
    );

    // Highlight helpers are used to highlight grid containers in dev mode. For more information, consult grid-tools.js.
    @if ($nested) {
        @include highlight-helper(
            'Nested Grid',
            $colour-nested-grids,
            '.show-containers'
        ) {
            @include grid-container-vertical-metrics(
                $margin-top: $column-margin-top
            );
        }
    } @else {
        @include highlight-helper('Grid', $colour-grids, '.show-containers') {
            @include grid-container-vertical-metrics(
                $margin-top: $column-margin-top
            );
        }
    }
}

/*
 * TODO Docs
 */

@mixin grid-container-horizontal-metrics(
    $nested: false,
    $use-horizontal-gutters: $use-horizontal-gutters,
    $padding-left: $default-container-padding-left,
    $padding-right: $default-container-padding-right
) {
    @if ($nested) {
        @if (
            $use-horizontal-gutters and
                is-valid-property-map($default-horizontal-base-unit)
        ) {
            @include fluid-property-map(
                $property: 'margin-left',
                $values: $default-horizontal-base-unit,
                $multiplier: -0.5
            );
            @include fluid-property-map(
                $property: 'margin-right',
                $values: $default-horizontal-base-unit,
                $multiplier: -0.5
            );
        }
    } @else {
        @if (is-valid-property-map($padding-left)) {
            @include fluid-property-map(
                $property: 'padding-left',
                $values: $padding-left
            );
        }

        @if (is-valid-property-map($padding-right)) {
            @include fluid-property-map(
                $property: 'padding-right',
                $values: $padding-right
            );
        }
    }
}

/*
 * TODO Docs
 */

@mixin grid-column(
    $width: null,
    $margin-left: null,
    $margin-right: null,
    $padding-left: $default-column-padding-left,
    $padding-right: $default-column-padding-right,
    $padding-top: $default-column-padding-top,
    $padding-bottom: $default-column-padding-bottom,
    $spill-direction: null,
    $use-horizontal-gutters: $default-use-vertical-gutters,
    $use-vertical-gutters: $default-use-vertical-gutters
) {
    display: block;
    position: relative;
    min-width: 0;

    // Width
    @if ($width != null) {
        @include grid-column-width($width);
    }

    @if ($default-horizontal-base-unit != null) {
        // Gutters
        @if (
            $padding-left !=
                null and
                $padding-left !=
                0 and
                $use-horizontal-gutters
        ) {
            @include grid-column-horizontal-padding($left: $padding-left);
        }

        @if (
            $padding-right !=
                null and
                $padding-left !=
                0 and
                $use-horizontal-gutters
        ) {
            @include grid-column-horizontal-padding($right: $padding-right);
        }

        // Margins
        @if (
            $spill-direction !=
                null and
                ($margin-left != null or $margin-right != null)
        ) {
            @error ('Horizontal margins and spill-direction cannot be set simultaneously on a column.');
        }

        @if ($margin-left != null) {
            @include grid-column-horizontal-metrics($left: $margin-left);
        }

        @if ($margin-right != null) {
            @include grid-column-horizontal-metrics($right: $margin-right);
        }

        // Spill
        @if $spill-direction != null {
            @include grid-column-spill($spill-direction);
        }
    }

    @if (
        $default-vertical-base-unit !=
            null and
            ($padding-top != 0 and $padding-bottom != 0) and
            $use-vertical-gutters
    ) {
        @include grid-container-vertical-metrics(
            $padding-top: $padding-top,
            $padding-bottom: $padding-bottom
        );
    }

    // Helpers
    @include highlight-helper('grid-column', $colour-columns, '.show-columns');

    & > * {
        @include highlight-helper(
            'grid-column > *',
            $colour-blocks,
            '.show-block-children'
        );
    }
}

/*
 * This mixin applies a width to a column based on a unitless fraction. It's designed to be agnostic to the number of
 * columns in the grid system you're using, so you can express your column's width as 4/12, 8/24, 1/2, 1/8 ...or 1/7 if you enjoy headaches.
 * NOTE: This mixin should not be used directly. Instead, access it by passing the above values to the grid-column mixin.
 */

@mixin grid-column-width($width: null) {
    @if (is-valid-property-map($width)) {
        $width: map-strip-unit($width);
        $width: map-percentage($width);
        @include property-map('width', $width);
    } @else if type-of($width) == 'number' {
        width: percentage($width);
    } @else {
        @error ("$width must be either a number or a valid property map.");
    }
}

/*
 * This mixin applies margins to a column based on a unitless fraction.
 * A parameter of $side: left and $width: 1/4, for example, would push your column to the
 * NOTE: This mixin should not be used directly. Instead, access it by passing the above values to the grid-column mixin.
 */

@mixin grid-column-horizontal-metrics($left: null, $right: null) {
    $margins: (
        left: $left,
        right: $right,
    );

    @each $side, $amount in $margins {
        @if (is-valid-property-map($amount)) {
            $amount: map-strip-unit($amount);
            $amount: map-percentage($amount);
            @include property-map('margin-#{$side}', $amount);
        } @else if type-of($amount) == 'number' {
            margin-#{$side}: percentage($amount);
        } @else if $amount != null {
            @debug (
                'A horizontal margin for a column must be a property map of fractions, or a number.'
            );
        }
    }
}

/*
 * TODO Docs
 */

@mixin grid-container-vertical-metrics(
    $nested: false,
    $padding-top: null,
    $padding-bottom: null,
    $margin-top: null,
    $margin-bottom: null
) {
    @include grid-vertical-metrics(
        $nested: $nested,
        $padding-top: $padding-top,
        $padding-bottom: $padding-bottom,
        $margin-top: $margin-top,
        $margin-bottom: $margin-bottom
    );
}

/*
 * TODO Docs
 */

@mixin grid-column-vertical-metrics(
    $padding-top: null,
    $padding-bottom: null,
    $margin-top: null,
    $margin-bottom: null
) {
    @include grid-vertical-metrics(
        $padding-top: $padding-top,
        $padding-bottom: $padding-bottom,
        $margin-top: $margin-top,
        $margin-bottom: $margin-bottom
    );
}

/*
 * TODO Docs
 */

@mixin grid-vertical-metrics(
    $nested: false,
    $padding-top: null,
    $padding-bottom: null,
    $margin-top: null,
    $margin-bottom: null
) {
    $properties: (
        padding-top: $padding-top,
        padding-bottom: $padding-bottom,
        margin-top: $margin-top,
        margin-bottom: $margin-bottom,
    );

    // Same as each loop used in grid-container-vertical-metrics-absolute.
    @each $key, $amount in $properties {
        @if type-of($amount) == 'number' {
            @include fluid-property-map(
                $property: $key,
                $values: $default-vertical-base-unit,
                $multiplier: $amount
            );
        } @else if $amount != null {
            @debug (
                'A vertical margin for a column must be a unitless number.'
            );
        }
    }
}

/*
 * This mixin applies top, bottom, left and right gutters to a column based on a unitless multiplier.
 * A parameter of $padding-top: 2, for example, would give the column a top-padding that is
 * 2 x the basic $gutter property map, set in your variables file.
 * NOTE: This mixin should not be used directly. Instead, access it by passing the above values to the grid-column mixin.
 */

@mixin grid-column-horizontal-padding($left: none, $right: none) {
    @if ($left != none and $left != 0) {
        @include fluid-property-map(
            $property: padding-left,
            $values: $default-horizontal-base-unit,
            $multiplier: $left
        );
    }

    @if ($right != none and $right != 0) {
        @include fluid-property-map(
            $property: padding-right,
            $values: $default-horizontal-base-unit,
            $multiplier: $right
        );
    }
}

/*
 * This mixin 'spills' a left-most or right-most column to the edge of the viewport, enabling you to have an inset grid system while still being able to reach the edges of the viewport with your blocks. Accepts either 'left' or 'right'.
 * NOTE: This mixin should not be used directly. Instead, access it by passing the above values to the grid-column mixin.
 */
@mixin grid-column-spill($direction) {
    @if ($direction != left and $direction != right and $direction != none) {
        @error ('Invalid spill direction value: #{$direction}');
    }

    @if $direction ==
        left and not
        is-valid-property-map($default-container-padding-left)
    {
        @error ('$default-container-padding-left must be set in order to spill a column to the left.');
    }

    @if $direction ==
        right and not
        is-valid-property-map($default-container-padding-right)
    {
        @error ('$default-container-padding-right must be set in order to spill a column to the right.');
    }

    $opposite: null;

    @if ($direction == left) {
        $opposite: right;
    } @else if($direction == right) {
        $opposite: left;
    } @else if($direction == top) {
        $opposite: bottom;
    } @else if($direction == bottom) {
        $opposite: top;
    } @else {
        @error ('Invalid direction: #{$direction}');
    }

    $padding: if(
        $direction == left,
        $default-container-padding-left,
        $default-container-padding-right
    );
    $distance: $padding;

    @if ($direction != none) {
        @include fluid-property-map(
            $property: #{$direction},
            $values: $distance,
            $multiplier: -1
        );

        > * {
            @include fluid-property-map(
                'margin-#{$opposite}',
                $values: $distance,
                $multiplier: -1
            );
        }
    } @else {
        #{$direction}: 0;
        margin-#{$opposite}: 0;
    }
}

/*
 * TODO Docs
 */

@mixin grid-column-spill-reverse($direction) {
    @if ($direction != left and $direction != right) {
        @error ('Invalid spill direction value: #{$direction}');
    }

    $distance: if(
        $direction == left,
        $default-container-padding-left,
        $default-container-padding-right
    );

    @if ($direction == left) {
        @include fluid-property-map('padding-left', $distance);
    } @else if($direction == right) {
        @include fluid-property-map('padding-right', $distance);
    }
}

/*
 * TODO Docs
 */

@mixin grid-overlay {
    @include grid;

    position: fixed;
    top: 0;
    left: 0;
    pointer-events: none;

    visibility: hidden;

    width: 100%; // Prevents collapse when flex parent

    z-index: 10000000;

    // These resets are to override the behaviour of the grid-container-vertical-metrics mixin.
    margin-top: 0 !important;
    margin-bottom: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;

    html.show-grid & {
        visibility: visible;
    }

    .show-containers &:after {
        display: none !important;
    }

    &__column {
        @include grid-column($width: 1 / $overlay-columns);

        // These resets are to override the behaviour of the grid-container-vertical-metrics mixin.
        margin-top: 0 !important;
        margin-bottom: 0 !important;
    }

    &__column:before {
        display: block;
        position: absolute;
        content: '';
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        border: 0.5px solid $colour-column-overlay;
    }

    &__column {
        .show-block-children & > *:after,
        .show-columns &:after {
            display: none !important;
        }
    }

    &__column > div {
        display: block;
        content: '';
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        height: 100vh;
        width: 100%;
        background-color: $colour-column-overlay;
    }
}

// Validate variables

@if (
    not
        map-keys-correspond-to-keys-in-other-map(
            $default-container-padding-left,
            $breakpoints
        )
) {
    @error ('The breakpoints in $default-container-padding-left do not all correspond to breakpoints in $breakpoints.');
}

@if (
    not
        map-keys-correspond-to-keys-in-other-map(
            $default-container-padding-right,
            $breakpoints
        )
) {
    @error ('The breakpoints in $default-container-padding-right do not all correspond to breakpoints in $breakpoints.');
}

@if (
    not
        maps-have-same-keys(
            $default-container-padding-left,
            $default-container-padding-right
        )
) {
    @error ('The breakpoint IDs in $default-container-padding-left and $default-container-padding-right do not match.');
}
