@use "sass:math"

@use "../utilities/derived-variables" as vars
@use "../utilities"

$column-gap: 0.75rem !default
$column-map: ('full': 100%, 'three-quarters': math.div(100% * 3, 4), 'two-thirds': math.div(100% * 2, 3), 'half': math.div(100%, 2), 'one-third': math.div(100%, 3), 'one-quarter': math.div(100%, 4), 'one-fifth': math.div(100%, 5), 'two-fifths': math.div(100% * 2, 5), 'three-fifths': math.div(100% * 3, 5), 'four-fifths': math.div(100% * 4, 5))

.column
  display: block
  flex-basis: 0
  flex-grow: 1
  flex-shrink: 1
  padding: $column-gap
  .columns.is-mobile > &.is-narrow
    flex: none
    width: unset
  @each $name, $value in $column-map
    .columns.is-mobile > &.is-#{$name}
      flex: none
      width: $value
  @each $name, $value in $column-map
    .columns.is-mobile > &.is-offset-#{$name}
      +utilities.ltr-property("margin", #{$value}, false)
  @for $i from 0 through 12
    .columns.is-mobile > &.is-#{$i}
      flex: none
      width: math.percentage(math.div($i, 12))
    .columns.is-mobile > &.is-offset-#{$i}
      +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)
  +utilities.mobile
    &.is-narrow-mobile
      flex: none
      width: unset
    @each $name, $value in $column-map
      &.is-#{$name}-mobile
        flex: none
        width: $value
    @each $name, $value in $column-map
      &.is-offset-#{$name}-mobile
        +utilities.ltr-property("margin", #{$value}, false)
    @for $i from 0 through 12
      &.is-#{$i}-mobile
        flex: none
        width: math.percentage(math.div($i, 12))
      &.is-offset-#{$i}-mobile
        +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)
  +utilities.tablet
    &.is-narrow,
    &.is-narrow-tablet
      flex: none
      width: unset
    @each $name, $value in $column-map
      &.is-#{$name},
      &.is-#{$name}-tablet
        flex: none
        width: $value

    @each $name, $value in $column-map
      &.is-offset-#{$name},
      &.is-offset-#{$name}-tablet
        +utilities.ltr-property("margin", #{$value}, false)
    @for $i from 0 through 12
      &.is-#{$i},
      &.is-#{$i}-tablet
        flex: none
        width: math.percentage(math.div($i, 12))
      &.is-offset-#{$i},
      &.is-offset-#{$i}-tablet
        +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)
  +utilities.touch
    &.is-narrow-touch
      flex: none
      width: unset
    @each $name, $value in $column-map
      &.is-#{$name}-touch
        flex: none
        width: $value

    @each $name, $value in $column-map
      &.is-offset-#{$name}-touch
        +utilities.ltr-property("margin", #{$value}, false)
    @for $i from 0 through 12
      &.is-#{$i}-touch
        flex: none
        width: math.percentage(math.div($i, 12))
      &.is-offset-#{$i}-touch
        +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)
  +utilities.desktop
    &.is-narrow-desktop
      flex: none
      width: unset
    @each $name, $value in $column-map
      &.is-#{$name}-desktop
        flex: none
        width: $value

    @each $name, $value in $column-map
      &.is-offset-#{$name}-desktop
        +utilities.ltr-property("margin", #{$value}, false)
    @for $i from 0 through 12
      &.is-#{$i}-desktop
        flex: none
        width: math.percentage(math.div($i, 12))
      &.is-offset-#{$i}-desktop
        +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)
  +utilities.widescreen
    &.is-narrow-widescreen
      flex: none
      width: unset
    @each $name, $value in $column-map
      &.is-#{$name}-widescreen
        flex: none
        width: $value

    @each $name, $value in $column-map
      &.is-offset-#{$name}-widescreen
        +utilities.ltr-property("margin", #{$value}, false)
    @for $i from 0 through 12
      &.is-#{$i}-widescreen
        flex: none
        width: math.percentage(math.div($i, 12))
      &.is-offset-#{$i}-widescreen
        +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)
  +utilities.fullhd
    &.is-narrow-fullhd
      flex: none
      width: unset
    @each $name, $value in $column-map
      &.is-#{$name}-fullhd
        flex: none
        width: $value

    @each $name, $value in $column-map
      &.is-offset-#{$name}-fullhd
        +utilities.ltr-property("margin", $value, false)
    @for $i from 0 through 12
      &.is-#{$i}-fullhd
        flex: none
        width: math.percentage(math.div($i, 12))
      &.is-offset-#{$i}-fullhd
        +utilities.ltr-property("margin", math.percentage(math.div($i, 12)), false)

.columns
  +utilities.ltr-property("margin", (-$column-gap), false)
  +utilities.ltr-property("margin", (-$column-gap))
  margin-top: (-$column-gap)
  &:last-child
    margin-bottom: (-$column-gap)
  &:not(:last-child)
    margin-bottom: calc(1.5rem - #{$column-gap})
  // Modifiers
  &.is-centered
    justify-content: center
  &.is-gapless
    +utilities.ltr-property("margin", 0, false)
    +utilities.ltr-property("margin", 0)
    margin-top: 0
    & > .column
      margin: 0
      padding: 0 !important
    &:not(:last-child)
      margin-bottom: 1.5rem
    &:last-child
      margin-bottom: 0
  &.is-mobile
    display: flex
  &.is-multiline
    flex-wrap: wrap
  &.is-vcentered
    align-items: center
  // Responsiveness
  +utilities.tablet
    &:not(.is-desktop)
      display: flex
  +utilities.desktop
    // Modifiers
    &.is-desktop
      display: flex

@if vars.$variable-columns
  .columns.is-variable
    --column-gap: 0.75rem
    +utilities.ltr-property("margin", calc(-1 * var(--column-gap)), false)
    +utilities.ltr-property("margin", calc(-1 * var(--column-gap)))
    > .column
      padding-left: var(--column-gap)
      padding-right: var(--column-gap)
    @for $i from 0 through 8
      &.is-#{$i}
        --column-gap: #{$i * 0.25rem}
      +utilities.mobile
        &.is-#{$i}-mobile
          --column-gap: #{$i * 0.25rem}
      +utilities.tablet
        &.is-#{$i}-tablet
          --column-gap: #{$i * 0.25rem}
      +utilities.tablet-only
        &.is-#{$i}-tablet-only
          --column-gap: #{$i * 0.25rem}
      +utilities.touch
        &.is-#{$i}-touch
          --column-gap: #{$i * 0.25rem}
      +utilities.desktop
        &.is-#{$i}-desktop
          --column-gap: #{$i * 0.25rem}
      +utilities.desktop-only
        &.is-#{$i}-desktop-only
          --column-gap: #{$i * 0.25rem}
      +utilities.widescreen
        &.is-#{$i}-widescreen
          --column-gap: #{$i * 0.25rem}
      +utilities.widescreen-only
        &.is-#{$i}-widescreen-only
          --column-gap: #{$i * 0.25rem}
      +utilities.fullhd
        &.is-#{$i}-fullhd
          --column-gap: #{$i * 0.25rem}
