import { _ as _extends } from '../extends-C4cduGrv.js';
import { _ as _defineProperty } from '../defineProperty-CEd-kZQW.js';
import { _ as _objectWithoutProperties } from '../objectWithoutProperties-TXwlfNxG.js';
import React__default from 'react';
import PropTypes from 'prop-types';

var _excluded = ["as", "columns", "gap", "children", "className", "style"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var arrayToColumns = function arrayToColumns(arr, cols) {
  var chunks = new Array(cols);

  for (var i = 0; i < arr.length; ++i) {
    var index = i % cols;

    if (!chunks[index]) {
      chunks[index] = [];
    }

    chunks[index].push(arr[i]);
  }

  return chunks;
};
var Masonry = /*#__PURE__*/React__default.forwardRef(function (_ref, ref) {
  var _ref$as = _ref.as,
      Component = _ref$as === void 0 ? "div" : _ref$as,
      columns = _ref.columns,
      _ref$gap = _ref.gap,
      gap = _ref$gap === void 0 ? "1" : _ref$gap,
      children = _ref.children,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? "" : _ref$className,
      style = _ref.style,
      props = _objectWithoutProperties(_ref, _excluded);

  var childrenArray = React__default.Children.toArray(children);
  var transformed = arrayToColumns(childrenArray, Number(columns));
  var classes = "grid gap-".concat(gap, " ").concat(className);

  var styles = _objectSpread({
    gridTemplateColumns: "repeat(".concat(columns, ", 1fr)")
  }, style);

  var refProp = Component instanceof Function ? {} : {
    ref: ref
  };
  return /*#__PURE__*/React__default.createElement(Component, _extends({}, props, {
    className: classes,
    style: styles
  }, refProp), transformed.map(function (elements, key) {
    return /*#__PURE__*/React__default.createElement("div", {
      key: key
    }, React__default.Children.map(elements, function (element, index) {
      var topMargin = index === 0 ? index : gap;
      return /*#__PURE__*/React__default.createElement("div", {
        className: "mb-0 mt-".concat(topMargin)
      }, element);
    }));
  }));
});
Masonry.propTypes = {
  as: PropTypes.elementType,
  gap: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  columns: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
  children: PropTypes.node.isRequired,
  className: PropTypes.string,
  style: PropTypes.object
};

export { arrayToColumns, Masonry as default };
