'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var cls = require('classnames');
var Progress_styles = require('./Progress.styles.js');
var Progress_tokens = require('./Progress.tokens.js');
var base = require('./variations/_view/base.js');
var base$1 = require('./variations/_size/base.js');
var base$2 = require('./variations/_progress-size/base.js');
var LabelWrapper = require('./ui/LabelWrapper/LabelWrapper.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var cls__default = /*#__PURE__*/_interopDefault(cls);

var _excluded = ["view", "status", "size", "progressSize", "value", "hasValue", "displayValue", "labelIcon", "labelTextPlacement", "label", "labelPlacement", "valuePlacement", "caption", "valueAlign", "className"];
var progressRoot = function progressRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, ref) {
    var view = _ref.view,
      status = _ref.status,
      size = _ref.size,
      progressSize = _ref.progressSize,
      value = _ref.value,
      _ref$hasValue = _ref.hasValue,
      hasValue = _ref$hasValue === void 0 ? true : _ref$hasValue,
      displayValue = _ref.displayValue,
      labelIcon = _ref.labelIcon,
      _ref$labelTextPlaceme = _ref.labelTextPlacement,
      labelTextPlacement = _ref$labelTextPlaceme === void 0 ? 'right' : _ref$labelTextPlaceme,
      label = _ref.label,
      _ref$labelPlacement = _ref.labelPlacement,
      labelPlacement = _ref$labelPlacement === void 0 ? 'top' : _ref$labelPlacement,
      _ref$valuePlacement = _ref.valuePlacement,
      valuePlacement = _ref$valuePlacement === void 0 ? 'right' : _ref$valuePlacement,
      caption = _ref.caption,
      _ref$valueAlign = _ref.valueAlign,
      valueAlign = _ref$valueAlign === void 0 ? 'start' : _ref$valueAlign,
      className = _ref.className,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var min = 0;
    var max = 100;
    var normalizedValue = Math.max(Math.min(value, max), min);
    var hasLabel = labelPlacement !== 'none' && (Boolean(label) || Boolean(labelIcon));
    var labelToValuePlacement = {
      left: 'right',
      top: 'top'
    };
    var resolvedValuePlacement = hasLabel ? labelToValuePlacement[labelPlacement] : valuePlacement;
    var isLabelTop = hasLabel && labelPlacement === 'top';
    var isLabelLeft = hasLabel && labelPlacement === 'left';
    var effectiveHasValue = displayValue !== undefined ? displayValue : hasValue;
    var showValue = effectiveHasValue && valuePlacement !== 'none';
    var isValueTop = showValue && resolvedValuePlacement === 'top';
    var isValueRight = showValue && resolvedValuePlacement === 'right';
    var showTopRow = isLabelTop || isValueTop;
    var showValueAlignOnTop = isValueTop && !isLabelTop;
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      ref: ref,
      view: status || view,
      size: size,
      progressSize: progressSize,
      value: normalizedValue,
      valuePlacement: resolvedValuePlacement,
      className: cls__default.default(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty({}, Progress_tokens.classes.labelPlacementLeft, isLabelLeft), Progress_tokens.classes.hasCaption, Boolean(caption)), Progress_tokens.classes.valueAlignStart, showValueAlignOnTop && valueAlign === 'start'), Progress_tokens.classes.valueAlignCenter, showValueAlignOnTop && valueAlign === 'center'), Progress_tokens.classes.valueAlignEnd, showValueAlignOnTop && valueAlign === 'end'), className)
    }, rest), showTopRow && /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledTopRow, null, isLabelTop && /*#__PURE__*/React__default.default.createElement(LabelWrapper.LabelWrapper, {
      labelIcon: labelIcon,
      labelTextPlacement: labelTextPlacement,
      label: label
    }), isValueTop && /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledValue, null, normalizedValue, "%")), /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledMidRow, null, isLabelLeft && /*#__PURE__*/React__default.default.createElement(LabelWrapper.LabelWrapper, {
      labelIcon: labelIcon,
      labelTextPlacement: labelTextPlacement,
      label: label
    }), /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledTrack, null, /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledProgress, {
      value: normalizedValue
    })), isValueRight && /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledValue, null, normalizedValue, "%")), caption && /*#__PURE__*/React__default.default.createElement(Progress_styles.StyledCaption, null, caption));
  });
};
var progressConfig = {
  name: 'Progress',
  tag: 'div',
  layout: progressRoot,
  base: Progress_styles.base,
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    },
    progressSize: {
      css: base$2.base
    }
  },
  defaults: {
    view: 'default',
    size: 'm',
    progressSize: '4'
  }
};

exports.progressConfig = progressConfig;
exports.progressRoot = progressRoot;
