//
// Copyright IBM Corp. 2016, 2018
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@import '../../globals/scss/vars';
@import '../../globals/scss/typography';
@import '../../globals/scss/helper-mixins';
@import '../../globals/scss/functions';
@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
@import '../form/form';
@import '../../globals/scss/css--reset';

@mixin checkbox {
  .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
    margin-bottom: $spacing-md;

    &:first-of-type {
      margin-top: $spacing-xs;
    }

    &:last-of-type {
      margin-bottom: 0;
    }
  }

  .#{$prefix}--checkbox {
    @include hidden;
  }

  .#{$prefix}--checkbox-label {
    @include reset;
    @include font-family;
    @include font-smoothing;
    @include typescale('zeta');
    display: flex;
    align-items: center;
    cursor: pointer;
    position: relative;
    padding-left: $spacing-lg;
    min-height: rem(16px);
  }

  .#{$prefix}--checkbox-label::before {
    box-sizing: border-box;
    content: '';
    position: absolute;
    left: 0;
    top: calc(50% - 9px);
    height: rem(18px);
    width: rem(18px);
    border: $checkbox-border-width solid $ui-05;
    background-color: $ui-01;
  }

  // Checkmark
  .#{$prefix}--checkbox-label::after {
    box-sizing: border-box;
    content: '';
    width: 9px;
    height: 5px;
    background: none;
    /*rtl:ignore*/
    border-left: 1px solid $inverse-01;
    border-bottom: 1px solid $inverse-01;
    /*rtl:ignore*/
    transform: scale(0) rotate(-45deg);
    position: absolute;
    left: rem(5px);
    top: 50%;
    margin-top: rem(-3px);
  }

  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::before {
    background-color: $brand-01;
    border-color: $brand-01;
  }

  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::after,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::after {
    opacity: 1;
    /*rtl:ignore*/
    transform: scale(1) rotate(-45deg);
  }

  .#{$prefix}--checkbox:not(:checked) + .#{$prefix}--checkbox-label::after {
    opacity: 0;
    /*rtl:ignore*/
    transform: scale(0) rotate(-45deg);
  }

  .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label__focus::before {
    @include focus-outline('blurred');
  }

  // Indeterminate dash
  .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::after,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::after {
    transform: scale(1) rotate(0deg);
    /*rtl:ignore*/
    border-left: 0 solid $inverse-01;
    border-bottom: 2px solid $inverse-01;
    opacity: 1;
    width: rem(10px);
    margin-top: rem(-4px);
    left: rem(4px);
  }

  //Disabled
  .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-label,
  .#{$prefix}--checkbox:disabled ~ .#{$prefix}--checkbox-label-text,
  .#{$prefix}--checkbox-label[data-contained-checkbox-disabled='true'] {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .#{$prefix}--checkbox-appearance {
    position: absolute;
    left: 0;
    top: calc(50% - 9px);
    display: inline-block;
    height: rem(18px);
    width: rem(18px);
    margin-right: $spacing-xs;
    background-color: $ui-01;
    border: $checkbox-border-width solid $ui-05;
    min-width: rem(18px);
  }

  // Backwards compatibilty for old checkbox html using .#{$prefix}--checkbox-appearance
  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance {
    top: rem(-1px);
  }

  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-appearance,
  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance {
    display: flex;
    align-items: baseline;
    background-color: $brand-01;
    border-color: $brand-01;
  }

  .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance,
  .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-appearance {
    @include focus-outline('blurred');
  }

  .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-appearance {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .#{$prefix}--checkbox-checkmark {
    display: none;
    fill: $inverse-01;
    width: 100%;
    height: 100%;
  }

  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-appearance .#{$prefix}--checkbox-checkmark,
  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance .#{$prefix}--checkbox-checkmark {
    display: block;

    @-moz-document url-prefix() {
      & {
        stroke: $brand-01;
      }
    }
  }

  // Skeleton state
  .#{$prefix}--checkbox-label.#{$prefix}--skeleton {
    @include skeleton;
    width: rem(100px);
    height: rem(18px);

    &:after,
    &:before {
      border: none;
    }
  }
}

@mixin checkbox--x {
  // Spacing between checkboxes
  .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
    margin-bottom: rem(8px);
  }

  // Spacing above collection of checkboxes
  .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper:first-of-type {
    margin-top: rem(3px);
  }

  // Remove spacing above collection of checkboxes if label is present
  .#{$prefix}--label + .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
    margin-top: 0;
  }

  // Spacing below collection of checkboxes
  .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper:last-of-type {
    margin-bottom: rem(3px);
  }

  // Visually, we'll hide the checkbox input and create our own styled version
  // to match the specs.
  .#{$prefix}--checkbox {
    @include hidden;
  }

  // The label corresponds to the content inside of the `label` tag. Since we're
  // creating our own checkbox style, we'll need to position this in order to
  // accomodate the spacing needed for the checkbox.
  .#{$prefix}--checkbox-label {
    @include reset;
    @include type-style('body-short-01');

    line-height: 1.5rem;
    position: relative;
    display: flex;
    cursor: pointer;
    padding-left: rem(26px); //width of checkbox 16px + 10px of padding
    min-height: rem(24px);
    user-select: none;
  }

  // Spacing for presentational checkbox
  .#{$prefix}--checkbox-label::before {
    box-sizing: border-box;
    content: '';

    // According to the spec, we'll want the bounding box for our checkbox to
    // be 16px. The border size will be what will be updated during the
    // different checkbox states.
    width: rem(16px);
    height: rem(16px);
    margin: rem(2px);

    // We need to position the pseudo-element absolutely in the space that we've
    // created with the padding from the label itself. We position only with
    // `top` since we don't want the checkbox to be centered vertically with the
    // text overflows.
    position: absolute;
    left: 0;
    top: rem(2px);

    // Checkboxes with a background color look visually off against a parent container.
    background-color: transparent;
    border: 1px solid $ui-05;
    border-radius: 1px;
  }

  // Create the appearance of the check in the `after` pseudo-element
  .#{$prefix}--checkbox-label::after {
    content: '';
    position: absolute;
    left: rem(6px);
    top: rem(8px);
    width: rem(7px);
    height: rem(3px);
    background: none;
    border-left: 2px solid $ui-02;
    border-bottom: 2px solid $ui-02;
    transform: scale(0) rotate(-45deg);
    transform-origin: bottom right;
    margin-top: rem(-3px);
  }

  //----------------------------------------------
  // Checked
  // ---------------------------------------------

  // Update properties for checked checkbox
  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::before {
    background-color: $text-01;
    border-color: $text-01;
    border-width: 1px;
  }

  // Display the check
  .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::after,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::after {
    transform: scale(1) rotate(-45deg);
  }

  // Indeterminate symbol
  .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::after,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::after {
    transform: scale(1) rotate(0deg);
    border-left: 0 solid $inverse-01;
    border-bottom: 2px solid $inverse-01;
    width: rem(8px);
    top: rem(11px);
  }

  //----------------------------------------------
  // Focus
  // ---------------------------------------------

  // Unchecked
  .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label__focus::before,
  // Checked
  .#{$prefix}--checkbox:checked:focus + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='true'].#{$prefix}--checkbox-label__focus::before,
  // Indeterminate
  .#{$prefix}--checkbox:indeterminate:focus + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed'].#{$prefix}--checkbox-label__focus::before {
    // We can't use outline here because of the rounded corners so have to increase the width/height to fake an outline.
    border-color: $interactive-01;
    border-width: 3px;
    width: rem(20px);
    height: rem(20px);
    left: -2px;
    top: 0;
  }

  //----------------------------------------------
  // Disabled
  // ---------------------------------------------

  // Workaround for: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/11295231
  [disabled] ~ _ {
    font-size: inherit;
  }

  .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-label,
  .#{$prefix}--checkbox-label[data-contained-checkbox-disabled='true'] {
    cursor: not-allowed;
    color: $disabled-02;
  }

  .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-disabled='true']::before {
    border-color: $disabled-02;
  }

  .#{$prefix}--checkbox:checked:disabled + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox:indeterminate:disabled + .#{$prefix}--checkbox-label::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='true'][data-contained-checkbox-disabled='true']::before,
  .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed'][data-contained-checkbox-disabled='true']::before {
    background-color: $disabled-02;
  }
}

@include exports('checkbox') {
  @if feature-flag-enabled('components-x') {
    @include checkbox--x;
  } @else {
    @include checkbox;
  }
}
