function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

/*
Copyright (c) 2018-2019 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { Block } from '../block/index.js';
import { mergeOverrides } from '../helpers/overrides.js';
import { getMediaQueries, getMediaQuery } from '../helpers/responsive-helpers.js';
export const flexGridItemMediaQueryStyle = ({
  $theme,
  flexGridColumnCount,
  flexGridColumnGap,
  flexGridRowGap
}) => {
  const colCount = flexGridColumnCount;
  const nthChild = colCount === 1 ? 'n' : colCount + 'n'; // 0px needed for calc() to behave properly

  const colGap = $theme.sizing[flexGridColumnGap] || flexGridColumnGap || '0px';
  const colGapQuantity = parseFloat(colGap);
  const colGapUnit = colGap.match(/[a-zA-Z]+/)[0];
  const rowGap = $theme.sizing[flexGridRowGap] || flexGridRowGap || '0px';
  const rowGapQuantity = parseFloat(rowGap);
  const widthCalc = `(100% - ${(colCount - 1) * colGapQuantity}${colGapUnit}) / ${colCount}`;
  const marginDirection = $theme.direction === 'rtl' ? 'marginLeft' : 'marginRight';
  return {
    // Subtract .5px to avoid rounding issues on IE/Edge
    // See https://github.com/uber/baseweb/pull/1748
    width: `calc(${widthCalc} - .5px)`,
    ...[...Array(colCount).keys()].reduce((acc, i) => {
      const nthChildOffset = i === 0 ? '' : '-' + i;
      return { // Iterate over each column i for 0 <= i < colCount
        ...acc,
        // Duplicate nth-child selector to match specificity of other selectors
        [`:nth-child(${nthChild}${nthChildOffset}):nth-child(${nthChild}${nthChildOffset})`]: {
          // Add colGap except at end of row
          [marginDirection]: i && colGapQuantity && colGap,
          // Add rowGap below in general
          marginBottom: rowGapQuantity && rowGap
        },
        [`:nth-child(${nthChild}${nthChildOffset}):last-child`]: {
          // Add space to make up for missing columns if row ends early
          [marginDirection]: `calc(${i} * (${colGap} + ${widthCalc}))`
        },
        ...[...Array(i + 1).keys()].reduce((acc, j) => ({ // Iterate over each column j for 0 <= j <= i
          ...acc,
          [`:nth-child(${nthChild}${nthChildOffset}):nth-last-child(${j + 1})`]: {
            // Remove rowGap below for last row items
            marginBottom: 0
          }
        }), {})
      };
    }, {})
  };
};
export const getResponsiveValue = (responsive, i) => {
  if (!responsive) {
    return null;
  }

  if (!Array.isArray(responsive)) {
    return responsive;
  }

  return responsive[i] || responsive[responsive.length - 1];
};
export const flexGridItemStyle = ({
  $flexGridColumnCount,
  $flexGridColumnGap,
  $flexGridRowGap,
  $theme
}) => {
  const baseFlexGridItemStyle = {
    flexGrow: 1
  };
  const mediaQueries = getMediaQueries($theme.breakpoints); // Get the length of the longest responsive array

  const maxResponsiveLength = Math.max(...[$flexGridColumnCount, $flexGridColumnGap, $flexGridRowGap].map(r => Array.isArray(r) ? r.length : 0)); // No media queries for non-responsive FlexGrids

  if (maxResponsiveLength < 2) {
    return { ...baseFlexGridItemStyle,
      ...flexGridItemMediaQueryStyle({
        $theme,
        flexGridColumnCount: getResponsiveValue($flexGridColumnCount, 0) || 1,
        flexGridColumnGap: getResponsiveValue($flexGridColumnGap, 0) || 0,
        flexGridRowGap: getResponsiveValue($flexGridRowGap, 0) || 0
      })
    };
  } // Generate style by iterating up to maxResponsiveLength for each responsive
  // breakpoint. Will end up with styles for one of the following media queries
  // depending on maxResponsiveLength:
  // - {mobile, small}
  // - {mobile, small, medium}
  // - {mobile, small, medium, large}


  return [...Array(maxResponsiveLength).keys()].reduce((acc, i) => {
    const [flexGridColumnCountValue, flexGridColumnGapValue, flexGridRowGapValue] = [$flexGridColumnCount, $flexGridColumnGap, $flexGridRowGap].map(r => getResponsiveValue(r, i));
    const mediaQuery = i === 0 ? // Custom media query needed so :nth-child styles don't conflict
    getMediaQuery({
      'min-width': '0px'
    }) : mediaQueries[i - 1];

    if (mediaQuery) {
      acc[mediaQuery] = flexGridItemMediaQueryStyle({
        $theme,
        flexGridColumnCount: flexGridColumnCountValue || 1,
        flexGridColumnGap: flexGridColumnGapValue || 0,
        flexGridRowGap: flexGridRowGapValue || 0
      });
    }

    return acc;
  }, baseFlexGridItemStyle);
};

const FlexGridItem = ({
  children,
  as,
  overrides,
  flexGridColumnCount,
  flexGridColumnGap,
  flexGridRowGap,
  ...restProps
}) => {
  const flexGridItemOverrides = {
    Block: {
      style: flexGridItemStyle
    }
  };
  const blockOverrides = overrides ? mergeOverrides(flexGridItemOverrides, overrides) : flexGridItemOverrides;
  return React.createElement(Block, _extends({
    as: as,
    overrides: blockOverrides,
    $flexGridColumnCount: flexGridColumnCount,
    $flexGridColumnGap: flexGridColumnGap,
    $flexGridRowGap: flexGridRowGap,
    "data-baseweb": "flex-grid-item"
  }, restProps), children);
};

export default FlexGridItem;