"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TemperatureControlDecorator = exports.TemperatureControlBase = exports.TemperatureControl = exports["default"] = void 0;

var _jsxRuntime = require("react/jsx-runtime");

var _kind = _interopRequireDefault(require("@enact/core/kind"));

var _Changeable = _interopRequireDefault(require("@enact/ui/Changeable"));

var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));

var _MeasurementFactory = _interopRequireDefault(require("ilib/lib/MeasurementFactory"));

var _UnitFmt = _interopRequireDefault(require("ilib/lib/UnitFmt"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _compose = _interopRequireDefault(require("ramda/src/compose"));

var _ArcSlider = _interopRequireDefault(require("../ArcSlider"));

var _Skinnable = _interopRequireDefault(require("../Skinnable"));

var _TemperatureControlModule = _interopRequireDefault(require("./TemperatureControl.module.css"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

/**
 * Temperature control base component.
 *
 * @class TemperatureControlBase
 * @memberof agate/TemperatureControl
 * @extends agate/ArcSlider.ArcSlider
 * @ui
 * @public
 */
var TemperatureControlBase = (0, _kind["default"])({
  name: 'TemperatureControl',
  propTypes:
  /** @lends agate/TemperatureControl.TemperatureControlBase.prototype */
  {
    /**
     * Disables TemperatureControl and becomes non-interactive.
     *
     * @type {Boolean}
     * @public
     */
    disabled: _propTypes["default"].bool,

    /**
     * The minimum value of the slider.
     *
     * @type {Number}
     * @default 30
     * @public
     */
    max: _propTypes["default"].number,

    /**
     * The minimum value of the slider.
     *
     * @type {Number}
     * @default 10
     * @public
     */
    min: _propTypes["default"].number,

    /**
     * Called when value is changed.
     *
     * @type {Function}
     * @public
     */
    onChange: _propTypes["default"].func,

    /**
     * The unit of the temperature(Celsius or Fahrenheit).
     *
     * @type {('Celsius'|'Fahrenheit')}
     * @default 'Celsius'
     * @public
     */
    unit: _propTypes["default"].oneOf(['Celsius', 'Fahrenheit']),

    /**
     * The value of the control.
     *
     * @type {Number}
     * @public
     */
    value: _propTypes["default"].number
  },
  defaultProps: {
    max: 30,
    min: 10,
    unit: 'Celsius',
    value: 15
  },
  styles: {
    css: _TemperatureControlModule["default"],
    className: 'temperatureControl',
    publicClassNames: true
  },
  render: function render(_ref) {
    var disabled = _ref.disabled,
        max = _ref.max,
        min = _ref.min,
        onChange = _ref.onChange,
        unit = _ref.unit,
        value = _ref.value,
        rest = _objectWithoutProperties(_ref, ["disabled", "max", "min", "onChange", "unit", "value"]);

    var currentTemperature = (0, _MeasurementFactory["default"])({
      unit: unit,
      amount: value
    });
    var currentTemperatureString = null;

    if (typeof window !== 'undefined') {
      var ufmt = new _UnitFmt["default"]({
        autoConvert: true,
        length: 'short',
        maxFractionDigits: 0,
        roundingMode: 'halfup'
      });
      currentTemperatureString = ufmt.format(currentTemperature);
    }

    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ArcSlider["default"], {
        "aria-valuetext": currentTemperatureString,
        backgroundColor: "#444444",
        className: _TemperatureControlModule["default"].slider,
        disabled: disabled,
        endAngle: 310,
        foregroundColor: value < min + (max - min) / 2 ? '#007aff' : '#f24949',
        max: max,
        min: min,
        onChange: onChange,
        slotCenter: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          children: currentTemperatureString
        }),
        startAngle: 50,
        value: value
      })
    }));
  }
});
/**
 * Applies Agate specific behaviors to [TemperatureControlBase]{@link agate/TemperatureControl.TemperatureControlBase}.
 *
 * @hoc
 * @memberof agate/TemperatureControl
 * @mixes ui/Changeable.Changeable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */

exports.TemperatureControlBase = TemperatureControlBase;
var TemperatureControlDecorator = (0, _compose["default"])(_Pure["default"], _Changeable["default"], _Skinnable["default"]);
/**
 * TemperatureControl with Agate styling
 * and [`TemperatureControlDecorator`]{@link agate/TemperatureControl.TemperatureControlDecorator}
 * applied.
 *
 * Usage:
 * ```
 * <TemperatureControl
 *   max={30}
 *   min={10}
 *   unit="Celsius"
 *   value={10}
 * />
 * ```
 *
 * @class TemperatureControl
 * @memberof agate/TemperatureControl
 * @extends agate/TemperatureControl.TemperatureControlBase
 * @mixes agate/TemperatureControl.TemperatureControlDecorator
 * @ui
 * @public
 */

exports.TemperatureControlDecorator = TemperatureControlDecorator;
var TemperatureControl = TemperatureControlDecorator(TemperatureControlBase);
exports.TemperatureControl = TemperatureControl;
var _default = TemperatureControl;
exports["default"] = _default;