@use "sass:string";
@use "sass:list";
@use "sass:map";

@use "../variables" as *;
@use "../utils" as *;

$rounded-attached-classes: ("default", "row", "view");

$rounded-corner-props: (
  "tl": "border-top-left-radius",
  "tr": "border-top-right-radius",
  "bl": "border-bottom-left-radius",
  "br": "border-bottom-right-radius",
);

// prettier-ignore
$rounded-sides: (
  "default": true,

  "top": ("tl","tr"),
  "bottom": ("bl", "br"),
  "left": ("tl","bl"),
  "right": ("tr","br"),
  
  "tl": ("tl"),
  "tr": ("tr"),
  "bl": ("bl"),
  "br": ("br"),
);

@mixin rounded-expand-properties($side-corners, $size) {
  @if type-of($side-corners) == "list" {
    @each $corner in $side-corners {
      $prop: map.get($rounded-corner-props, $corner);
      #{$prop}: $size !important;
    }
  } @else {
    @if $side-corners == true {
      border-radius: $size !important;
    } @else {
      $prop: map.get($rounded-corner-props, $side-corners);
      #{$prop}: $size !important;
    }
  }
}

@function rounded-selector($class-prefix: "") {
  $selectors: ();

  @each $ext-class in $rounded-attached-classes {
    $prefix: if($ext-class != "default", "." + $ext-class, "");
    $selector: "&#{$prefix}.r#{$class-prefix}";
    $selectors: list.append($selectors, $selector);
  }

  @return join($selectors, ", ");
}

.mie {
  @each $side, $side-corners in $rounded-sides {
    $side-short-key: if(
      string.length($side) == 2,
      $side,
      string.slice($side, 1, 1)
    );

    $round-prefix: if($side != "default", $side-short-key, "");

    @each $size-name, $size in $rounded-size {
      $size-postfix: if($size-name != "medium", "-" + $size-name, "");
      $class-prefix: $round-prefix + $size-postfix;
      $selector: rounded-selector($class-prefix);

      #{$selector} {
        @include rounded-expand-properties($side-corners, $size);
      }

      &.select.r#{$class-prefix} {
        > .content {
          > .base {
            @include rounded-expand-properties($side-corners, $size);
          }
        }
      }

      &.entry.r#{$class-prefix} {
        @include rounded-expand-properties($side-corners, $size);

        > .field {
          > input,
          > textarea {
            @include rounded-expand-properties($side-corners, $size);
          }
        }
      }
    }
  }
}
