'use strict';

var React = require('react');
var _commonjsHelpers = require('./_commonjsHelpers-cjs.js');
var _getTag = require('./_getTag-cjs.js');
var isTypedArray$1 = require('./isTypedArray-cjs.js');
var DataList_const = require('./DataList.const-cjs.js');
var DataListTags = require('./DataListTags-cjs.js');
var DataListHeaderTile = require('./DataListHeaderTile-cjs.js');
var FormatDate = require('./FormatDate-cjs.js');
var Text = require('./Text-cjs.js');
var Heading = require('./Heading-cjs.js');

var baseKeys = _getTag._baseKeys,
    getTag = _getTag._getTag,
    isArguments = isTypedArray$1.isArguments_1,
    isArray = isTypedArray$1.isArray_1,
    isArrayLike = isTypedArray$1.isArrayLike_1,
    isBuffer = isTypedArray$1.isBufferExports,
    isPrototype = isTypedArray$1._isPrototype,
    isTypedArray = isTypedArray$1.isTypedArray_1;

/** `Object#toString` result references. */
var mapTag = '[object Map]',
    setTag = '[object Set]';

/** Used for built-in method references. */
var objectProto = Object.prototype;

/** Used to check objects for own properties. */
var hasOwnProperty = objectProto.hasOwnProperty;

/**
 * Checks if `value` is an empty object, collection, map, or set.
 *
 * Objects are considered empty if they have no own enumerable string keyed
 * properties.
 *
 * Array-like values such as `arguments` objects, arrays, buffers, strings, or
 * jQuery-like collections are considered empty if they have a `length` of `0`.
 * Similarly, maps and sets are considered empty if they have a `size` of `0`.
 *
 * @static
 * @memberOf _
 * @since 0.1.0
 * @category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is empty, else `false`.
 * @example
 *
 * _.isEmpty(null);
 * // => true
 *
 * _.isEmpty(true);
 * // => true
 *
 * _.isEmpty(1);
 * // => true
 *
 * _.isEmpty([1, 2, 3]);
 * // => false
 *
 * _.isEmpty({ 'a': 1 });
 * // => false
 */
function isEmpty(value) {
  if (value == null) {
    return true;
  }
  if (isArrayLike(value) &&
      (isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
        isBuffer(value) || isTypedArray(value) || isArguments(value))) {
    return !value.length;
  }
  var tag = getTag(value);
  if (tag == mapTag || tag == setTag) {
    return !value.size;
  }
  if (isPrototype(value)) {
    return !baseKeys(value).length;
  }
  for (var key in value) {
    if (hasOwnProperty.call(value, key)) {
      return false;
    }
  }
  return true;
}

var isEmpty_1 = isEmpty;

var isEmpty$1 = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(isEmpty_1);

/**
 * Return the child component that matches the `type` provided
 */
function getCompoundComponent(children, type) {
    const childrenArray = React.Children.toArray(children);
    const element = childrenArray.find(child => React.isValidElement(child) && child.type === type);
    // Comply with the return type without casting it
    return React.isValidElement(element) ? element : undefined;
}
/**
 * Return all instances child component that matches the `type` provided
 */
function getCompoundComponents(children, type) {
    const childrenArray = React.Children.toArray(children);
    const elements = childrenArray.filter((child) => React.isValidElement(child) && child.type === type);
    return elements;
}
/**
 * Generate the default element the DataList would use on the data provided.
 */
function generateListItemElement(item) {
    return Object.keys(item).reduce((acc, key) => {
        const currentItem = item[key];
        if (!currentItem) {
            return acc;
        }
        if (key === "tags" && Array.isArray(currentItem)) {
            acc[key] = React.createElement(DataListTags.DataListTags, { items: currentItem });
        }
        else if (key === "label" && typeof currentItem === "string") {
            acc[key] = React.createElement(Heading.Heading, { level: 5 }, currentItem);
        }
        else if (React.isValidElement(currentItem)) {
            acc[key] = currentItem;
        }
        else if (currentItem instanceof Date) {
            acc[key] = (React.createElement(Text.Text, null,
                React.createElement(FormatDate.FormatDate, { date: currentItem })));
        }
        else {
            acc[key] = React.createElement(Text.Text, null, currentItem);
        }
        return acc;
    }, {});
}
/**
 * Generate the header elements with the default styling
 */
function generateHeaderElements(headers) {
    const headerElements = Object.keys(headers).reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: React.createElement(DataListHeaderTile.DataListHeaderTile, { headers: headers, headerKey: key }) })), {});
    return isEmpty$1(headerElements) ? undefined : headerElements;
}
function sortBreakpoints(sizeProp) {
    return sizeProp.sort((a, b) => DataList_const.BREAKPOINTS.indexOf(a) - DataList_const.BREAKPOINTS.indexOf(b));
}
function getExposedActions(childrenArray, childCount = 2) {
    const firstNChildren = childrenArray.slice(0, childCount);
    return firstNChildren.reduce((result, child, i) => {
        const hasIcon = Boolean(child.props.icon);
        const isAlwaysVisible = child.props.alwaysVisible; // If true, the child action will always be visible and not nested in the dropdown.
        if (isAlwaysVisible === false ||
            (isAlwaysVisible === undefined && !hasIcon)) {
            return result;
        }
        const isLastChildAdded = result.length === i;
        // If it's the first child or if the previous child was added, then add this child.
        if (i === 0 || (i < childCount && isLastChildAdded)) {
            return [...result, child];
        }
        return result;
    }, []);
}

exports.generateHeaderElements = generateHeaderElements;
exports.generateListItemElement = generateListItemElement;
exports.getCompoundComponent = getCompoundComponent;
exports.getCompoundComponents = getCompoundComponents;
exports.getExposedActions = getExposedActions;
exports.sortBreakpoints = sortBreakpoints;
