"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var React = _interopRequireWildcard(require("react"));
var _LinearProgressBarCoreSt = require("./LinearProgressBarCore.st.css.js");
var _DataHooks = require("./DataHooks");
var _filterDataProps = require("./utils/filter-data-props");
var _excluded = ["min", "max", "role"];
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/LinearProgressBar/LinearProgressBarCore/LinearProgressBarCore.tsx",
  _this = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var FULL_PROGRESS = 100;
var NO_PROGRESS = 0;
var resolveIndicationElement = props => {
  var wrapped = (dataHook, children) => /*#__PURE__*/React.createElement("div", {
    "data-hook": dataHook,
    className: _LinearProgressBarCoreSt.classes.indicationContainer,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 52,
      columnNumber: 5
    }
  }, children);
  if (props.error && props.errorIcon) {
    return wrapped(_DataHooks.ProgressBarDataHooks.errorIcon, /*#__PURE__*/React.createElement("span", {
      className: _LinearProgressBarCoreSt.classes.progressIcon,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 60,
        columnNumber: 7
      }
    }, props.errorIcon));
  }
  if (props.value === FULL_PROGRESS && props.successIcon) {
    return wrapped(_DataHooks.ProgressBarDataHooks.successIcon, /*#__PURE__*/React.createElement("span", {
      className: _LinearProgressBarCoreSt.classes.progressIcon,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 67,
        columnNumber: 7
      }
    }, props.successIcon));
  }
  if (props.label) {
    return wrapped(_DataHooks.ProgressBarDataHooks.suffixIndicator, /*#__PURE__*/React.createElement("span", {
      className: _LinearProgressBarCoreSt.classes.suffixIndication,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 74,
        columnNumber: 7
      }
    }, props.label));
  }
  return wrapped(_DataHooks.ProgressBarDataHooks.progressPercentage, /*#__PURE__*/React.createElement("span", {
    className: _LinearProgressBarCoreSt.classes.progressPercentage,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 80,
      columnNumber: 5
    }
  }, "".concat(props.value, "%")));
};
var renderBarSection = function renderBarSection(value) {
  var isButton = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var ariaLabel = arguments.length > 2 ? arguments[2] : undefined;
  var progressWidth = {
    width: "".concat(value, "%")
  };
  var Component = isButton ? 'button' : 'div';
  return /*#__PURE__*/React.createElement(Component, {
    "data-hook": _DataHooks.ProgressBarDataHooks.container,
    className: _LinearProgressBarCoreSt.classes.barContainer,
    "aria-label": ariaLabel,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 92,
      columnNumber: 5
    }
  }, /*#__PURE__*/React.createElement("div", {
    "data-hook": _DataHooks.ProgressBarDataHooks.background,
    className: _LinearProgressBarCoreSt.classes.barBackground,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 97,
      columnNumber: 7
    }
  }), /*#__PURE__*/React.createElement("div", {
    "data-hook": _DataHooks.ProgressBarDataHooks.foreground,
    style: progressWidth,
    className: _LinearProgressBarCoreSt.classes.barForeground,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 101,
      columnNumber: 7
    }
  }));
};
var getRelativeValue = props => {
  var {
    value = 0,
    min = 0,
    max = 100,
    precision
  } = props;
  var relativeValue = +((Number(value) - min) / (max - min) * 100).toFixed(precision || 1);
  return precision ? relativeValue : parseInt(relativeValue, 10);
};
var normalizeProps = props => {
  var {
    value,
    min,
    max
  } = props;
  if (value !== undefined && max !== undefined && +value >= max) {
    return _objectSpread(_objectSpread({}, props), {}, {
      value: FULL_PROGRESS
    });
  }
  if ([undefined, null, ''].includes(value) || value !== undefined && min !== undefined && +value < min) {
    return _objectSpread(_objectSpread({}, props), {}, {
      value: NO_PROGRESS
    });
  }
  return _objectSpread(_objectSpread({}, props), {}, {
    value: getRelativeValue(props)
  });
};
var getDataAttributes = props => {
  var {
    value,
    min,
    max
  } = props;
  return {
    [_DataHooks.ProgressBarDataKeys.value]: value,
    [_DataHooks.ProgressBarDataKeys.min]: min,
    [_DataHooks.ProgressBarDataKeys.max]: max
  };
};
var getAriaAttributes = props => {
  return {
    [_DataHooks.ProgressBarAriaKeys.valuenow]: props.value !== undefined ? +props.value : NO_PROGRESS,
    [_DataHooks.ProgressBarAriaKeys.valuemin]: props.min !== undefined ? +props.min : props.min,
    [_DataHooks.ProgressBarAriaKeys.valuemax]: props.max !== undefined ? +props.max : props.max,
    [_DataHooks.ProgressBarAriaKeys.valuetext]: props[_DataHooks.ProgressBarAriaKeys.valuetext]
  };
};
var LinearProgressBarCore = _ref => {
  var {
      min = NO_PROGRESS,
      max = FULL_PROGRESS,
      role = 'progressbar'
    } = _ref,
    propsWithNoDefaults = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var {
    error,
    showProgressIndication,
    prefixIndication,
    label,
    className,
    onClick
  } = propsWithNoDefaults;
  var allProps = _objectSpread({
    min,
    max,
    role
  }, propsWithNoDefaults);
  var _props = normalizeProps(_objectSpread({
    min,
    max,
    role
  }, propsWithNoDefaults));
  var success = _props.value === FULL_PROGRESS;
  return /*#__PURE__*/React.createElement("div", (0, _extends2.default)({}, getDataAttributes(_props), role === 'progressbar' ? getAriaAttributes(allProps) : {}, {
    "data-min": _props.min,
    "data-error": error
  }, role ? {
    role
  } : {}, {
    className: (0, _LinearProgressBarCoreSt.st)(_LinearProgressBarCoreSt.classes.root, {
      error,
      success
    }, className)
  }, (0, _filterDataProps.filterDataProps)(allProps), {
    onClick: onClick,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 194,
      columnNumber: 5
    }
  }), prefixIndication && /*#__PURE__*/React.createElement("div", {
    "data-hook": _DataHooks.ProgressBarDataHooks.prefixIndicator,
    className: _LinearProgressBarCoreSt.classes.prefixIndicationContainer,
    "aria-hidden": allProps['aria-hide-affixes'],
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 205,
      columnNumber: 9
    }
  }, prefixIndication), renderBarSection(_props.value, !!onClick, allProps['aria-label']), (showProgressIndication || label) && /*#__PURE__*/React.createElement("div", {
    "data-hook": _DataHooks.ProgressBarDataHooks.progressIndicator,
    className: _LinearProgressBarCoreSt.classes.progressIndicationSection,
    "aria-hidden": allProps['aria-hide-affixes'],
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 216,
      columnNumber: 9
    }
  }, resolveIndicationElement(_props)));
};
LinearProgressBarCore.displayName = 'LinearProgressBar';
var _default = exports.default = LinearProgressBarCore;
//# sourceMappingURL=LinearProgressBarCore.js.map