/**
 * @file
 * Styles for the managed file widget.
 *
 * This includes the styles for the file widgets and the image widgets.
 */

@import "../base/variables.pcss.css";

:root {
  --file-widget-form-item-min-width: 16rem;
  --file-widget-form-item-max-width: 32rem;
}

/**
 * The root element of the file/image widget.
 */
.form-managed-file.no-upload {
  display: inline-flex;
  flex-direction: column;
  max-width: 100%;
}

.form-managed-file.has-value.is-multiple {
  display: block;
}

/**
 * The main element of the file/image widget.
 *
 * This contains the upload input and the upload of the empty file/image
 * widgets, or the file name (with icon and size) and the remove button of
 * filled widgets.
 *
 * The inline-flex display shrinks the width to the minimal needed amount. This
 * helps to keep the remove as close to the other elements as possible.
 */
.form-managed-file__main {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
}

/**
 * Inside (draggable) tables, this should be flex-displayed. This keeps even
 * long file names in the same visual line where the drag handle is.
 */
.draggable .form-managed-file.has-value .form-managed-file__main {
  display: flex;
}

/* Add some bottom margin for single widgets if no meta is present. */
.form-managed-file.is-single.has-value .form-managed-file__main:last-child {
  margin-bottom: var(--space-m);
}

/**
 * The 'meta' element of the file/image widget.
 *
 * This element is available only if the file widget has a value AND when there
 * are other input options than the ones rendered in the 'main' element.
 * These inputs are:
 *  - File description and/or the file display checkbox of file widgets
 *  - Image alt and/or title text as well as the preview image of the image
 *    widgets.
 *
 * The trick here is that we will display the alt/title inputs next to the image
 * preview if there is enough space left. Enough space means the value of the
 * '--file-widget-form-item-min-width' variable.
 */
.form-managed-file__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  margin-top: var(--space-m); /* Bottom margin will be added by the child elements: because of we use flex display here, our margins won't collapse. */
}

/**
 * In tables, this should be inline-flex. This is needed to make this element be
 * pushed to a new line, to the bottom of the drag handle.
 */
.draggable .form-managed-file.has-value .form-managed-file__meta {
  display: inline-flex;
}

/**
 * Internet Explorer 11 does not shrinks our meta elements if one of its parent
 * element is a table. Without this fix, the file widgets table cell would have
 * the same width that is needed for displaying the file name in a single line.
 *
 * @see https://github.com/philipwalton/flexbugs/issues/179
 */
@media all and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  *::-ms-backdrop,
  td .form-managed-file__meta {
    flex-basis: 100%;
  }
}

/**
 * The 'image preview' element.
 *
 * This is used and display only by the image widget.
 */
.form-managed-file__image-preview {
  flex: 0 0 auto;
  max-width: 100%;
  margin-bottom: var(--space-m);
}

/* Add some 'end' margin if there are other meta inputs. */
.form-managed-file.has-meta .form-managed-file__image-preview {
  margin-right: var(--space-m); /* LTR */
}
[dir="rtl"] .form-managed-file.has-meta .form-managed-file__image-preview {
  margin-right: 0;
  margin-left: var(--space-m);
}

/**
 * If this is rendered inside a file multiple table and there are no alt or
 * title, we have to reduce the amount of the bottom margin.
 */
td .form-managed-file.no-meta .form-managed-file__image-preview {
  margin-bottom: var(--space-xs);
}

/**
 * The children of the 'meta items' element are the inputs that were described
 * at the 'meta' element '.form-managed-file__meta', except of the image
 * preview.
 *
 * The flex-basis is set to the minimal width where we can display these inputs
 * next tho the preview image.
 *
 * We limit the max width directly on the '.form-item' elements.
 */
.form-managed-file__meta-items {
  flex: 1 1 var(--file-widget-form-item-min-width);
  max-width: 100%;
}

/**
 * Internet Explorer 11 does not increase the width of those flex items that are
 * allowed to be wrapped. We just simply change the basis to the max-width.
 */
@media all and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  *::-ms-backdrop,
  .form-managed-file__meta-items {
    flex-basis: var(--file-widget-form-item-max-width);
  }
}

/**
 * Meta items wrapper.
 * This markup element is needed only for working around the same IE 11 bug that
 * is described above, at the 'meta' element.
 *
 * @see https://github.com/philipwalton/flexbugs/issues/179
 */
@media all and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  *::-ms-backdrop,
  td .form-managed-file__meta-wrapper {
    display: flex;
  }
}

/**
 * Modify component defaults for file/image widgets.
 */

/**
 * File component style overrides for managed file widgets.
 */
.form-managed-file .file {
  word-break: break-all;
  hyphens: auto;
}

.form-managed-file .file__size {
  word-break: normal;
}

.form-managed-file__main .file {
  flex: 1 1 auto;
  margin: var(--space-xs) var(--space-m) var(--space-xs) 0; /* LTR */
}
[dir="rtl"] .form-managed-file__main .file {
  margin-right: 0;
  margin-left: var(--space-m);
}

/**
 * Fix and override the table-related bug of Internet Explorer 11 (described at
 * the 'meta' element).
 *
 * @see https://github.com/philipwalton/flexbugs/issues/179
 */
@media all and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  *::-ms-backdrop,
  td .form-managed-file__main .file {
    flex: 0 1 100%;
  }
}

/**
 * The file upload input.
 */
.form-managed-file__main .form-element--api-file {
  flex: 1 1 auto;
  min-width: 1px; /* This makes the element to be shrinked on IE11 */
}

/**
 * Remove the default button margins and prevent shrinking or growing buttons.
 * This applies both on the 'no-js' upload button and the remove button. The
 * weight of this ruleset has been increase for this to take effect on RTL.
 */
.form-managed-file__main .button.button {
  flex: 0 0 auto;
  margin: 0;
}

/**
 * Limiting the width of form items inside the meta element.
 */
.form-managed-file__meta .form-item {
  max-width: var(--file-widget-form-item-max-width);
  margin-top: 0; /* Top margin is added by the parent element */
  margin-bottom: var(--space-m);
}

/**
 * Reduce the bottom margin of the last 'meta' form-item for field multiple
 * tables.
 */
.form-managed-file__meta .form-item:last-child {
  margin-bottom: var(--space-xs);
}

.form-managed-file__meta .form-element {
  width: 100%;
}

/**
 * Add side margins if a table precedes the managed file form element.
 */
.file-widget-multiple.has-table .form-type--managed-file {
  margin-right: var(--space-m);
  margin-left: var(--space-m);
}
