@use "sass:map";
@use "sass:math";

/**
 * Column
 *
 * examples :
 *      column(6) 			-> return 6 column + 5 gutter of desktop preset
 * 		column('m', 6) 		-> return 6 column + 5 gutter of mobile preset
 * 		column(6, 5, 8)		-> return 6 column + 5 gutter of 8 grid size of desktop
 *
 */

// ----
// column-set-var
// ----

@function column-set-var($var, $device, $prop) {

    @if not $var {

        @return map.get(map.get($column-preset, $device), $prop);
    } @else {

        @return $var;
    }
}

// ----
// column - return a percent value
// ----

@function column($device, $nb-column: null, $nb-gutter: null, $total-column: null, $total-gutter: null) {
    // shift vars if $device is number
    @if type-of($device) == "number" {
        $total-gutter: $total-column;
        $total-column: $nb-gutter;
        $nb-gutter: $nb-column;
        $nb-column: $device;
        $device: d;
    }

    $preset: map.get($column-preset, $device);
    $gutter-width: map.get($preset, gutter-width);
    $column-width: map.get($preset, column-width);
    $total-column: column-set-var($total-column, $device, total-column);

    @if not $nb-gutter {
        $nb-gutter: $nb-column - 1;
    }

    @if not $total-gutter {
        $total-gutter: $total-column - 1;
    }

    @return math.div($nb-column * $column-width + $nb-gutter * $gutter-width, $total-column * $column-width + $total-gutter * $gutter-width) * 100%;
}

// ----
// column px, return a pixel value
// ----

@function column-px($device, $nb-column: null, $nb-gutter: null, $unitless: false) {
    // shift vars if $device is number
    @if type-of($device) == "number" {
        $nb-gutter: $nb-column;
        $nb-column: $device;
        $device: d;
    }

    $preset: map.get($column-preset, $device);
    $gutter-width: map.get($preset, gutter-width);
    $column-width: map.get($preset, column-width);

    @if not $nb-gutter {
        $nb-gutter: $nb-column - 1;
    }

    @if $unitless == true {
        @return $nb-column * $column-width + $nb-gutter * $gutter-width;
    }

    @return #{$nb-column * $column-width + $nb-gutter * $gutter-width}px;
}

// ----
// column full - return a percent value of the full width container (100%) minus mobile external gutter width * 2
// ----

@function column-full($device, $nb-column: null, $nb-gutter: null) {
    // shift vars if $device is number
    @if type-of($device) == "number" {
        $nb-gutter: $nb-column;
        $nb-column: $device;
        $device: d;
    }

    $preset: map.get($column-preset, $device);
    $gutter-width: map.get($preset, gutter-width);
    $column-width: map.get($preset, column-width);
    $total-column: map.get($preset, total-column);

    $width: column-px($device, $nb-column, $nb-gutter, true);

    @return calc((100% - var(--external-gutter) * 2) * #{math.div($width, ($gutter-width * ($total-column - 1) + $column-width * $total-column))});
}

// ----
// column factor
// ----

@function column-factor($device, $nb-column: null, $nb-gutter: null, $total-column: null, $total-gutter: null) {
    @return math.div(to-number(strip-unit(column($device, $nb-column, $nb-gutter, $total-column, $total-gutter))), 100);
}
