///
//  Copyright (c) 2023 GrowStocks
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.
///

@use '../kardia';
@use 'sass:list';
@use 'form-field-base' as base;
@use 'form-field-theme' as theme;

@mixin render(
  $color: (),
  $struct: (),
  $typography: (),
  $settings: ()
) {
  $color: kardia.theme-merge(theme.$init-color, $color);
  $struct: kardia.theme-merge(theme.$init-struct, $struct);
  $typography: kardia.theme-merge(theme.$init-typography, $typography);
  $settings: kardia.theme-merge(theme.$init-settings, $settings);

  $resolve-padding: if(list.length(kardia.theme-get($struct, 'padding')) >= 2, list.nth(kardia.theme-get($struct, 'padding'), 2), list.nth(kardia.theme-get($struct, 'padding'), 1));

  @include base.init(
    0.7rem,
    kardia.primitive-token-switch('padding.#{$resolve-padding}', $resolve-padding),
    kardia.primitive-token-switch('padding.#{list.nth(kardia.theme-get($struct, 'padding'), 1)}', list.nth(kardia.theme-get($struct, 'padding'), 1)),
  ) {
    .gs-form-field__label {
      @include kardia.color-bind('form-field-label', (
        ink: kardia.theme-get($color, 'ink', 'disabled'),
      ));
      @include kardia.typography-bind('form-field-label', (
        family: kardia.theme-get($typography, 'family'),
        size: kardia.theme-get($typography, 'size', 'label'),
        weight: kardia.theme-get($typography, 'weight'),
        line-height: kardia.theme-get($typography, 'height'),
      ));
    }

    .gs-form-field__input {
      @include kardia.struct-bind('form-field', (
        width: kardia.theme-get($struct, 'width'),
        padding: kardia.theme-get($struct, 'padding'),
        radius: kardia.theme-get($struct, 'radius'),
        border-width: kardia.theme-get($struct, 'border-width'),
        border-style: kardia.theme-get($struct, 'border-style'),
        gap: kardia.theme-get($struct, 'gap'),
      ));
      @include kardia.typography-bind('form-field', (
        family: kardia.theme-get($typography, 'family'),
        size: kardia.theme-get($typography, 'size', 'default'),
        weight: kardia.theme-get($typography, 'weight'),
        line-height: kardia.theme-get($typography, 'height'),
      ));

      &:not(:read-only),
      &:not(:disabled) {
        @include kardia.color-bind('form-field', (
          fill: kardia.theme-get($color, 'fill'),
          ink: kardia.theme-get($color, 'ink'),
          border: kardia.theme-get($color, 'fill'),
        ));

        &:focus {
          @include kardia.color-bind('form-field', (
            border: kardia.theme-get($color, 'border', 'focus'),
          ));
          @include kardia.struct-bind('form-field', (
            border-width: kardia.theme-get($struct, 'border-width', 'focus'),
          ));

          + .gs-form-field__label {
            @include kardia.color-bind('form-field-label', (
              fill: kardia.theme-get($color, 'fill'),
            ));
          }
        }

        + .gs-form-field__label {
          @include kardia.color-bind('form-field-label', (
            ink: kardia.theme-get($color, 'ink'),
          ));
        }
      }

      &:read-only,
      &:disabled {
        @include kardia.color-bind('form-field', (
          fill: kardia.theme-get($color, 'fill', 'disabled'),
          border: kardia.theme-get($color, 'fill', 'disabled'),
        ));
      }
    }

    .gs-form-field__helper {
      padding: 0.3rem kardia.primitive-token-switch('padding.#{$resolve-padding}', $resolve-padding) 0;
      @include kardia.color-bind('form-field-helper', (
        ink: kardia.theme-get($color, 'ink', 'helper'),
      ));
      @include kardia.typography-bind('form-field-helper', (
        family: kardia.theme-get($typography, 'family'),
        size: kardia.theme-get($typography, 'size', 'helper'),
        weight: kardia.theme-get($typography, 'weight'),
        line-height: kardia.theme-get($typography, 'height'),
      ));
    }

    &:where(.is-error) {
      .gs-form-field__input:not(:read-only),
      .gs-form-field__input:not(:disabled) {
        @include kardia.color-bind('form-field', (
          ink: 'error-400',
        ));

        &:focus {
          @include kardia.color-bind('form-field', (
            border: 'error-400'
          ));
        }

        + .gs-form-field__label {
          @include kardia.color-bind('form-field-label', (
            ink: 'error-400',
          ));

          + .gs-form-field__helper {
            @include kardia.color-bind('form-field-helper', (
              ink: 'error-400',
            ));
          }
        }
      }

      .gs-form-field__label {
        @include kardia.color-bind('form-field-label', (
          ink: kardia.theme-get($color, 'ink', 'disabled'),
        ));
      }

      .gs-form-field__helper {
        @include kardia.color-bind('form-field-helper', (
          ink: kardia.theme-get($color, 'ink', 'helper'),
        ));
      }
    }

    &:where(.is-raised) {
      @include kardia.struct-bind('form-field', (
        shadow: 'xs',
      ));
    }

    &:where(.is-control) {
      $target-dimension: kardia.theme-get($typography, 'size');

      .gs-form-field__label,
      .gs-form-field__input,
      .gs-form-field__helper {
        width: auto;
        position: static;
        padding: 0;
      }

      .gs-form-field__label,
      .gs-form-field__input:focus + .gs-form-field__label {
        background-color: unset;
        @include kardia.color-bind('form-field-label', (
          ink: kardia.theme-get($color, 'ink', 'helper') !important,
        ));
        border-color: unset;
        font-size: unset;
      }

      .gs-form-field__label {
        width: 100%;
        user-select: none;
        cursor: default;
      }

      .gs-form-field__input[type='radio'],
      .gs-form-field__input[type='checkbox'] {
        width: kardia.token-switch('size-#{$target-dimension}', kardia.primitive-token-switch('size.#{$target-dimension}'));
        height: kardia.token-switch('size-#{$target-dimension}', kardia.primitive-token-switch('size.#{$target-dimension}'));
        margin-right: kardia.primitive-token-get('spacing.xs');
        cursor: pointer;
      }

      .gs-form-field__helper {
        margin-left: calc(calc(#{kardia.token-switch('size-#{$target-dimension}', kardia.primitive-token-switch('size.#{$target-dimension}'))} + 3px) + #{kardia.primitive-token-get('spacing.xs')});
        user-select: none;
      }
    }
  }
}