"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.object.define-properties.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/web.dom-collections.for-each.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrCheckbox = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = require("react");
require("./index.css");
var _classnames = _interopRequireDefault(require("classnames"));
var _icon = require("../icon");
var _type = require("../icon/type");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["label", "onChange", "styleType", "size", "indeterminate", "style", "checkIconAlignRight"];
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 FcrCheckbox = exports.FcrCheckbox = function FcrCheckbox(props) {
  var label = props.label,
    onChange = props.onChange,
    _props$styleType = props.styleType,
    styleType = _props$styleType === void 0 ? 'brand' : _props$styleType,
    _props$size = props.size,
    size = _props$size === void 0 ? 'medium' : _props$size,
    indeterminate = props.indeterminate,
    style = props.style,
    _props$checkIconAlign = props.checkIconAlignRight,
    checkIconAlignRight = _props$checkIconAlign === void 0 ? false : _props$checkIconAlign,
    inputProps = (0, _objectWithoutProperties2["default"])(props, _excluded);
  var cls = (0, _classnames["default"])('fcr-checkbox', "fcr-checkbox-".concat(styleType), {
    'fcr-checkbox-s': size === 'small',
    'fcr-checkbox-check-icon-align-right': checkIconAlignRight
  });
  var _useState = (0, _react.useState)(inputProps.checked || false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    checked = _useState2[0],
    setChecked = _useState2[1];
  var checkboxRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    setChecked(inputProps.checked || false);
  }, [inputProps.checked]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
    className: cls,
    style: _objectSpread({}, style),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
      className: "fcr-checkbox-input-wrapper",
      onClick: function onClick(e) {
        if (checkboxRef.current) {
          var currentChecked = checkboxRef.current.checked;
          onChange === null || onChange === void 0 || onChange(!currentChecked);
          setChecked(!currentChecked);
        }
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", _objectSpread(_objectSpread({}, inputProps), {}, {
        checked: inputProps.checked,
        ref: checkboxRef,
        type: "checkbox",
        onChange: function onChange(e) {},
        onClick: function onClick(e) {
          // prevent the second event from firing
          e.stopPropagation();
        }
      })), /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
        className: (0, _classnames["default"])('fcr-btn-click-effect fcr-checkbox-inner', {
          'fcr-checkbox-inner-indeterminate': indeterminate && !checked
        }),
        children: [indeterminate && !checked && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          className: "fcr-checkbox-input-indeterminate",
          type: _type.FcrIconType.FCR_CHEXBOX_INDETERMINATE,
          size: size === 'small' ? 8 : 12,
          colors: {
            iconPrimary: styleType === 'brand' ? 'var(--fcr_ui_scene_white10)' : 'var(--fcr_ui_scene_ramp_brand6)'
          }
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          type: _type.FcrIconType.FCR_CHECKBOX_CHECK,
          size: size === 'small' ? 8 : 12,
          colors: {
            iconPrimary: styleType === 'brand' ? 'var(--fcr_ui_scene_white10)' : 'var(--fcr_ui_scene_icontext1)'
          }
        })]
      })]
    }), label && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "fcr-checkbox-label",
      onClick: function onClick(e) {
        onChange === null || onChange === void 0 || onChange(!checked);
        setChecked(!checked);
      },
      children: label
    })]
  });
};