@use "sass:map";
@use "../variables/units";

// Original spacing of dso-button-row
// ----------------------------------
// To Do: re-evaluate vertical rhythm
:where(.dso-button-row) {
  margin-block: units.$u2 units.$u4;

  &:last-child {
    margin-block-end: 0;
  }
}

// within dso-highlight-box, create space for dso-button-row when preceded and/or followed by other elements
// ---------------------------------------------------------------------------------------------------------
// To Do: integrate this with things to come within this file, and remove the wildcard!
:where(dso-highlight-box),
:where(.dso-highlight-box) {
  :where(.dso-button-row) {
    margin-block: 0;
  }

  :where(*) + :where(.dso-button-row),
  :where(.dso-button-row) + :where(*) {
    margin-block-start: units.$block-spacing-medium;
  }
}

dso-accordion {
  .dso-rich-content + dso-accordion,
  dso-accordion + .dso-rich-content {
    margin-block-start: units.$u2;
  }
}

// TEMP fix for excessive whitespace within the accordion rich content
// To do: a more permanent solution for vertical rhythm!
.dso-accordion-section {
  // specificity increase necessary..
  &.dso-accordion-default,
  &.dso-accordion-compact,
  &.dso-accordion-neutral,
  &.dso-accordion-compact-black {
    .dso-rich-content {
      ul ul {
        margin-block-end: 0;
      }
      > :first-child {
        margin-block-start: 0;
      }

      > :last-child {
        margin-block-end: 0;
      }
    }
  }
}

dso-accordion-section {
  *:first-child:not(.dso-info) {
    margin-block-start: 0;
  }

  > *:last-child:not(.dso-info) {
    margin-block-end: 0;
  }
}

// #2440 temp fix
// --------------
// To do: integrate this with #2217 (103)
:where(.row:has(.dso-button-row)) {
  + :where(.row:has(:only-child > dl)) {
    margin-block-start: units.$block-spacing-medium;
  }
}

// https://github.com/dso-toolkit/dso-toolkit/issues/2989
.dso-app-heading + * {
  margin-block-start: units.$block-spacing-medium;
}

:where(*) + :where(.dso-form-buttons),
:where(.dso-form-buttons) + :where(*) {
  margin-block: units.$block-spacing-xlarge;
}

dso-legend-item + dso-legend-item {
  margin-block-start: units.$block-spacing-small;
}

$spacers: (
  (
    space: units.$block-spacing-small,
    predecessors: (
      ".dso-context-wrapper",
      "dso-list-button",
    ),
    successors: (
      ".dso-table-responsive",
      "dso-list-button",
    ),
  ),
  (
    space: units.$block-spacing-medium,
    predecessors: (
      ".dso-app-heading",
    ),
    successors: (
      ".dso-context-wrapper",
      "dso-tabs",
    ),
  ),
  (
    space: units.$block-spacing-xlarge,
    predecessors: (
      "dso-action-list",
      "dso-contact-information",
    ),
    successors: (
      ".dso-form-buttons",
      "dso-contact-information",
    ),
  )
);

@each $spacer in $spacers {
  $space: map.get($spacer, space);
  $predecessors: map.get($spacer, predecessors);
  $successors: map.get($spacer, successors);

  :where(#{$predecessors}) + :where(#{$successors}, :has(#{$successors})) {
    margin-block-start: $space;
  }
}
