"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _reactMeasure = _interopRequireDefault(require("react-measure"));

var _constants = require("../../constants");

var _VerticalContext = _interopRequireWildcard(require("../VerticalContext"));

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/**
 * `Columns` component is used for creating of multi-column layouts.
 * @since 2.6.0
 */
const Columns = ({
  baseClassName = "".concat(_constants.CLS_PREFIX, "columns"),
  className,
  children,
  vertical: verticalProp,
  gap,
  ...props
}) => {
  // eslint-disable-next-line prefer-const
  let [vertical, setVertical] = (0, _react.useState)(false);
  const verticalContext = (0, _react.useContext)(_VerticalContext.default);

  if (verticalProp === undefined) {
    verticalProp = typeof verticalContext === 'undefined' ? _VerticalContext.VERTICAL_BREAKPOINT : verticalContext;
  }

  if (typeof verticalProp === 'boolean') {
    vertical = verticalProp;
  }

  const handleResize = ({
    bounds
  }) => {
    setVertical(bounds.width < verticalProp);
  };

  const createRoot = measureRef => /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    ref: measureRef,
    className: (0, _classnames.default)(baseClassName, {
      ["".concat(baseClassName, "--vertical")]: vertical,
      ["".concat(baseClassName, "--gap-").concat(gap)]: gap
    }, className)
  }, props), /*#__PURE__*/_react.default.createElement(_VerticalContext.default.Provider, {
    value: vertical
  }, children));

  if (typeof verticalProp === 'number') {
    return /*#__PURE__*/_react.default.createElement(_reactMeasure.default, {
      bounds: true,
      onResize: handleResize
    }, ({
      measureRef
    }) => createRoot(measureRef));
  }

  return createRoot();
};

var _default = Columns;
exports.default = _default;