
//------------------------------------*\
//  # Alignment
//------------------------------------*/

// Clearfix
//
@mixin clearfix() {
    &::after {
        clear: both;
        content: '';
        display: block;
    }
}


// Display inline block
//
// @param $width - element width
// @param $height - element height
//
@mixin inlineBlock($width: false, $height: false) {
    display: inline-block;

    @if ($width != false) {
        width: $width;
    }

    @if ($height != false) {
        height: $height;
    }
}


//------------------------------------*\
//  # Display
//------------------------------------*/

// Display block
//
// @param $width
// @param $height
//
@mixin block($width: false, $height: false) {
    display: block;

    @if ($width != false) {
        width: $width;
    }

    @if ($height != false) {
        height: $height;
    }
}

// Display block and set bottom margin
//
// @param $margin - margin bottom
// @param $width - element width
// @param $height - element height
//
@mixin spacedBlock($margin: $blockMarginBottom, $width: false, $height: false) {
    display: block;
    margin-bottom: $margin;

    @if ($width != false) {
        width: $width;
    }

    @if ($height != false) {
        height: $height;
    }
};


//------------------------------------*\
//  # Content
//------------------------------------*/

// Prefix
//
// @param $value
// @param $margin - right margin
// @param $font - font family name
// @param $color - hex value or color var
//
@mixin prefix($value: '-', $margin: .5em, $font: false, $color: false) {
    &::before {
        content: $value;
        margin-right: $margin;

        @if ($font != false) {
            @include font($font);
        }

        @if ($color != false) {
            color: $color;
        }

    }
}


// Suffix
//
// @param $value
// @param $margin - left margin
// @param $font - font family name
// @param $color - hex value or color var
//
@mixin suffix($value: '-', $margin: .5em, $font: false, $color: false) {
    &::after {
        content: $value;
        margin-left: $margin;

        @if ($font != false) {
            @include font($font);
        }

        @if ($color != false) {
            color: $color;
        }

    }
}

// Bookends
//
// @param $value
// @param $margin - left and right margin
// @param $font - font family name
// @param $color - hex value or color
//
@mixin bookends($value: '-', $margin: .5em, $font: false, $color: false) {
    @include prefix($value, $margin, $font, $color);
    @include suffix($value, $margin, $font, $color);
}


//------------------------------------*\
//  # Blocks
//------------------------------------*/

// A block level element, centered with margin
//
// @param $maxWidth - max width of container
// @param $margin - left and right margin
//
@mixin centeredBlock($maxWidth: false, $margin: false) {
    display: block;
    margin-left: auto;
    margin-right: auto;

    @if ($maxWidth != false) {
        max-width: $maxWidth;
    }

    @if ($margin != false) {
        margin-bottom: $margin;
    }
}


//------------------------------------*\
//  # Sizing
//------------------------------------*/

// Set image aspect ratio
//
// @param $keyword - embed
// @param $ratio - proportion between two unitless values
//
@mixin ratio($keyword: false, $ratio: 16 / 9) {
    @if ($keyword != false and $keyword == embed) {
        overflow: hidden;
        position: relative;
        &::before {
            content: '';
            display: block;
            height: 0;
            padding-top: ((1 / $ratio) * 100%);
        }
    } @else {
        // Shift arguments
        @if ($keyword != false) {
            $ratio: $keyword;
        }

        display: block;
        height: 0;
        padding-top: ((1 / $ratio) * 100%);
    }
}

// Set width and/or height
//
// @param $width - element width
// @param $height - element height
//
@mixin size($width, $height: false) {
    width: $width;

    @if ($height != false) {
        height: $height
    } @else {
        height: $width;
    }
}

// Output max-width and/or max-height
//
// @param $width - container max width
// @param $height - container max height
//
@mixin maxSize($width, $height: false) {
    max-width: $width;

    @if ($height != false) {
        max-height: $height
    } @else {
        max-height: $width;
    }
}

// Output min-width and/or min-height
//
// @param $width - container min width
// @param $height - container min height
//
@mixin minSize($width, $height: false) {
    min-width: $width;

    @if ($height != false) {
        min-height: $height
    } @else {
        min-height: $width;
    }
}


//------------------------------------*\
//  # Shapes
//------------------------------------*/

// Creates a circle
//
// @param $diameter - width and height of element
// @param $crop - overflow hidden true/false
// @param $display - element display property
//
@mixin circle($diameter, $crop: false, $display: block) {
    @include rounded($diameter / 2);

    @if (unit($diameter) == '') {
        $diameter: $diameter + rem;
    }

    height: $diameter;
    width: $diameter;

    @if ($crop != false) {
        overflow: hidden;
    }

    @if ($display != false) {
        display: $display;
    }
}

// Creates a triangle
//
// @param $keyword - direction of triangle
// @param $color - hex value or color var
// @param size - triangle size
// @param width - triangle width
//
@mixin triangle($keyword, $color: $darkGray, $size: 5px, $width: $size) {
    content: '';
    height: 0;
    width: 0;

    @if ($keyword == up or $keyword == down) {
        border-left: $width solid transparent;
        border-right: $width solid transparent;

        @if ($keyword == up) {
            border-bottom: $size solid $color;
        } @else {
            border-top: $size solid $color;
        }
    }

    @if ($keyword == left or $keyword == right) {
        border-bottom: $width solid transparent;
        border-top: $width solid transparent;

        @if ($keyword == left) {
            border-right: $size solid $color;
        } @else {
            border-left: $size solid $color;
        }
    }
}

//------------------------------------*\
//  # Font Loading
//------------------------------------*/

// Load font
//
// @param name
// @param $file
// @param $weight
// @param $style
//
@mixin loadFont($name, $file: $name, $weight: normal, $style: normal) {
    @font-face {
        font-family: #{$name};
        font-weight: $weight;
        font-style: $style;

        @if ($woff2Enabled) {
            src: url('#{$fontPath}#{$file}.woff2') format('woff2'),
                 url('#{$fontPath}#{$file}.woff') format('woff'),
                 url('#{$fontPath}#{$file}.ttf') format('truetype');
        } @else {
            src: url('#{$fontPath}#{$file}.woff') format('woff'),
                 url('#{$fontPath}#{$file}.ttf') format('truetype');
        }
    }
}


//------------------------------------*\
//  # Grid
//------------------------------------*/

// Grid row
//
// @param margin - margin left
//
@mixin row($margin: $gridMargin) {
    @include clearfix();

    margin-left: ($margin * -1);
    max-width: (100% + $margin);
}

// Grid row modify
//
// @param margin - margin left
//
@mixin rowModify($margin: $gridMargin) {
    margin-left: ($margin * -1);
    max-width: (100% + $margin);
}

// Grid row reset
//
@mixin rowReset() {
    margin-left: 0;
    max-width: none;
}

// Grid column
//
// @param $keyword - set margin between "spaced" columns
// @param $share - share/span of total columns
// @param $columns - number of columns
// @param $margin - if "spaced", sets margin left
//
@mixin column($keyword: false, $share: false, $columns: $gridColumns, $margin: $gridMargin) {
    float: left;

    @if ($keyword != false) {
        @if ($keyword == spaced) {
            margin-left: $margin;
            width: ((100% / $columns) * $share) - $margin;
        } @else {
            $args: $keyword, $share, $columns, $margin;

            @if (type-of($keyword) == 'number') {
                @if (unit($keyword) == '%') {
                    width: $keyword;
                } @else {
                    $newColumns: $columns;
                    $newShare: $keyword;

                    @if ($keyword and $share != false) {
                        $newColumns: $share;
                    }

                    width: (100% / $newColumns * $newShare);
                }
            }
        }
    } @else {
        width: 100%;
    }
}

// Modify grid column
//
// @param $keyword - set margin between columns "spaced"
// @param $share - share/span of total columns
// @param $columns - number of columns
// @param $margin - if "spaced", sets margin left
//
@mixin columnModify($keyword: false, $share: false, $columns: $gridColumns, $margin: $gridMargin) {
    @if ($keyword != false) {
        @if ($keyword == spaced) {
            margin-left: $margin;
            width: ((100% / $columns) * $share) - $margin;
        } @else {
            // If keyword is not "spaced", shift values
            $columns: $share;
            $share: $keyword;

            width: ((100% / $columns) * $share);
        }
    }
}

// Reset grid column
//
// @param $resetMargin - Reset left margin
//
@mixin columnReset($resetMargin: false) {
    float: none;
    width: auto;

    @if ($resetMargin != false) {
        margin-left: 0;
    }
}

// Pull grid column
//
// @param $share - share/span of total columns
// @param $columns - number of columns
//
@mixin columnPull($share, $columns: $gridColumns) {
    position: relative;
    right: ((100% / $columns) * $share);
}

// Push grid column
//
// @param $share - share/span of total columns
// @param $columns - number of columns
//
@mixin columnPush($share, $columns: $gridColumns) {
    left: ((100% / $columns) * $share);
    position: relative;
}

// Column Offset
//
// @param $keyword - set margin between columns "spaced"
// @param $share - share/span of total columns
// @param $columns - number of columns
// @param $margin - if "spaced", sets margin left
//
@mixin columnOffset($keyword: false, $share: false, $columns: $gridColumns, $margin: ($gridMargin / 2)) {
    @if ($keyword == spaced) {
        margin-left: (((100% / $columns) * $share) + ($margin * 2));
    } @else {
        @if (type-of($keyword) == number) {
            margin-left: (100% * ($keyword / $share));
        } @else {
            margin-left: ((100% / $columns) * $share);
        }
    }
}


//------------------------------------*\
//  # Inline Grid
//------------------------------------*/

// Inline grid row
//
// @param $margin - left margin
// @param $spaceless - Add whitespace hack
//
@mixin inlineRow($margin: $gridMargin, $spaceless: $gridSpaceless) {
    margin-left: ($margin * -1);
    max-width: (100% + $margin);

    @if ($spaceless != true) {
        letter-spacing: -.32em;
    }
}

// Inline grid column
//
// @param $keyword - set margin between columns "spaced"
// @param $share - share/span of total columns
// @param $columns - number of columns
// @param $margin - if "spaced", sets margin left
// @param spaceless
//
@mixin inlineColumn($keyword: false, $share: false, $columns: $gridColumns, $margin: $gridMargin, $spaceless: $gridSpaceless) {
    display: inline-block;
    vertical-align: top;

    @if ($keyword == spaced) {
        margin-left: $margin;
        width: ((100% / $columns) * $share) - $margin;

        @if (not $spaceless) {
            letter-spacing: normal;
        }
    } @else if ($keyword != false and type-of($keyword) == number) {
        $args: $keyword, $share, $columns, $margin, $spaceless;

        @if (type-of(nth($args, 3)) == bool) {
            $spaceless: nth($args, 3);
        }

        @if (nth($args, 2) != false) {
            $columns: nth($args, 2);
        }

        $share: nth($args, 1);

        width: (100% / $columns) * $share;

        @if (not $spaceless) {
            margin-right: -.32em;
        }
    }
}


//------------------------------------*\
//  # Text Styling
//------------------------------------*/

// Ellipsis
//
// @param $maxWidth - max width of container
//
@mixin ellipsis($maxWidth: false) {
    overflow-x: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    @if ($maxWidth != false) {
        max-width: $maxWidth;
    }
}


//------------------------------------*\
//  # Other
//------------------------------------*/

// Hide text
//
@mixin hideText() {
    overflow: hidden;
    text-indent: 110%;
    white-space: nowrap;
}

// Output font icon
//
// @param $icon - font icon variable
// @param $size - font size
// @param $rotate - degree/angle to rotate icon
// @param $weight - font weight
// @param $sharpen - true or false
// @param $font - font family name
//
@mixin icon(
    $icon,
    $size: inherit,
    $rotate: false,
    $weight: normal,
    $lineHeight: 0,
    $sharpen: true,
    $font: $iconFont
) {
    @include font($font, $size, $weight, $lineHeight, normal);

    content: "#{$icon}";
    display: inline-block;

    @if ($rotate != false) {
        transform: rotate($rotate);
    }

    @if ($sharpen != false) {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}

// Modify font icon
//
// @param $icon - font icon variable
// @param $size - font size
// @param $rotate - degree/angle to rotate icon
// @param $weight - font weight
// @param $sharpen - true or false
//
@mixin iconModify($icon: false, $size: false, $rotate: false, $weight: false, $sharpen: false) {
    @if ($icon != false) {
        content: "#{$icon}";
    }

    @if ($size != false) {
        font-size: $size;
    }

    @if ($rotate != false) {
        transform: rotate($rotate)
    }

    @if ($weight != false) {
        font-weight: $weight;
    }

    @if ($sharpen != false) {
        @include textSharpen();
    }
}

// Text Sharpen
//
@mixin textSharpen() {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

// Selection
//
// @param $color - hex value or color var
// @param $background - selection background color
//
// TODO: Double check autoprefixer on this one
@mixin selection($color: $selectionColor, $background: $selectionBackground) {
    &::selection {
        background: $background;
        color: $color;
        text-shadow: none;
    }
}

// Hide clear/cancel button in search inputs
//
@mixin noClear() {
    &::-ms-clear {
        display: none;
    }
    &::-webkit-search-cancel-button {
        -webkit-appearance: none;
    }
}

//------------------------------------*\
//  # Positioning
//------------------------------------*/

// Helper for positioning
//
// @param $type - absolute, relative, or fixed
// @param $top - top position of el
// @param $right - right position of el
// @param $bottom - bottom position of el
// @param $left - left position of el
// @private
//
@mixin position($type, $top: 0, $right: 0, $bottom: 0, $left: 0) {
    position: $type;

    @each $data in top $top, right $right, bottom $bottom, left $left {
        @if (nth($data, 2) != false) {
            #{nth($data, 1)}: nth($data, 2);
        }
    }
}

// Absolute positioning
//
// @param $top - top position of el
// @param $right - right position of el
// @param $bottom - bottom position of el
// @param $left - left position of el
//
@mixin absolute(
    $top: false,
    $right: false,
    $bottom: false,
    $left: false
) {
    @include position(absolute, $top, $right, $bottom, $left);
}

// Relative positioning
//
// @param $top - top position of el
// @param $right - right position of el
// @param $bottom - bottom position of el
// @param $left - left position of el
//
@mixin relative(
    $top: false,
    $right: false,
    $bottom: false,
    $left: false
) {
    @include position(relative, $top, $right, $bottom, $left);
}

// Fixed positioning
//
// @param $top - top position of el
// @param $right - right position of el
// @param $bottom - bottom position of el
// @param $left - left position of el
//
@mixin fixed(
    $top: false,
    $right: false,
    $bottom: false,
    $left: false
) {
    @include position(fixed, $top, $right, $bottom, $left);
}


//------------------------------------*\
//  # Font Selection
//------------------------------------*/

// Font setting
//
// @param $family - font family name
// @param $size - font size
// @param $weight - font weight
// @param $lineHeight - amount of space used for lines
// @param $style - normal, italic, or oblique face
// @param $spacing - amount of space between letters
//
@mixin font(
    $family: $baseFont,
    $size: false,
    $weight: false,
    $lineHeight: false,
    $style: false,
    $spacing: false
) {
    @if ($family != false) {
        font-family: #{$family};
    }

    @if ($size != false) {
        font-size: $size;
    }

    @if ($weight != false) {
        font-weight: $weight;
    }

    @if ($lineHeight != false) {
        line-height: $lineHeight;
    }

    @if ($style != false) {
        font-style: $style;
    }

    @if ($spacing != false) {
        letter-spacing: $spacing;
    }
}

//------------------------------------*\
//  # Heading
//------------------------------------*/

// Default styling for headings
//
@mixin heading($fontSize: false) {
    @include font($headingFont, $fontSize, $headingFontWeight, $headingLineHeight);

    color: $headingColor;
    margin-bottom: $headingMarginBottom;
    small {
        font-weight: normal;
    }
}

//------------------------------------*\
//  # Rounded Corners
//------------------------------------*/

// Border radius
//
// @param $keyword - top, right, bottom, left
// @param $radius - value defines how rounded el's corners are
//
@mixin rounded($keyword: '', $value: $defaultRadius) {
    $corners: false;

    @if ($keyword != false) {
        background-clip: border-box;

        @if ($keyword == left or $keyword == right) {
            $corners: border-top-#{$keyword}-radius,
                border-bottom-#{$keyword}-radius;
        } @else if ($keyword == top or $keyword == bottom) {
            $corners: border-#{$keyword}-left-radius,
                border-#{$keyword}-right-radius;
        }

        @if ($corners != false) {
            @each $corner in $corners {
                #{$corner}: $value;
            }
        } @else {
            @if (type-of($keyword) == number) {
                $value: $keyword;
            }

            @if (unit($value) == '') {
                $value: $value + rem;
            }

            border-radius: $value;
        }
    }
}


//------------------------------------*\
//  # Resizable
//------------------------------------*/

// Sets overflow to hidden and resize to both
//
// @param $value - el is resizable if so, which direction
//
@mixin resizable($value: both) {
    overflow: hidden;
    resize: $value;
}


//------------------------------------*\
//  # Placeholder
//------------------------------------*/

// Styles form placeholder text
//
@mixin placeholder($color: $inputPlaceholderColor) {
    @include optional-at-root('::-webkit-input-placeholder') {
        color: $color;
        @content;
    }

    @include optional-at-root(':-moz-placeholder') {
        color: $color;
        @content;
    }

    @include optional-at-root('::-moz-placeholder') {
        color: $color;
        @content;
    }

    @include optional-at-root(':-ms-input-placeholder') {
        color: $color;
        @content;
    }
}


//------------------------------------*\
//  # Flexbox
//------------------------------------*/

@mixin flex($grow: 0, $shrink: 0, $basis: auto) {
    flex-grow: $grow;
    flex-shrink: $shrink;
    flex-basis: $basis;
}

@mixin flexContainer($direction: row, $wrap: nowrap, $justify: flex-start, $align: stretch, $alignContent: stretch) {
    display: flex;
    flex-direction: $direction;
    flex-wrap: $wrap;
    justify-content: $justify;
    align-items: $align;
    align-content: $alignContent;
}


//------------------------------------*\
//  # Utility
//------------------------------------*/

@mixin optional-at-root($sel) {
    @at-root #{if(not &, $sel, selector-append(&, $sel))} {
        @content;
    }
}
