"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _omit2 = _interopRequireDefault(require("ramda/src/omit"));
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _ = require("../../..");
var _utils = require("../../../common/utils");
var _Checkbox = require("./Checkbox.shape");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var CHECKBOX_DEFAULT_SIZE = 5;
var checkIfHasError = function checkIfHasError(error) {
  return (0, _utils.isString)(error) && error !== '';
};
var CleanInput = _react.default.forwardRef(function (props, ref) {
  return _react.default.createElement("input", _extends({
    ref: ref
  }, (0, _omit2.default)(['hasError', 'visuallyFocused'], props)));
});
var TooltipIcon = (0, _styledComponents.default)(_.OldIcon.Questionmark).withConfig({
  displayName: "Checkbox__TooltipIcon",
  componentId: "sc-vu58cg-0"
})(["display:inline;vertical-align:middle;margin-left:", "px;"], function (p) {
  return p.theme.spacing.unit(1);
});
var getSize = function getSize(size) {
  if (size && size === 's') {
    return 4;
  }
  return CHECKBOX_DEFAULT_SIZE;
};
var CheckmarkBox = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "Checkbox__CheckmarkBox",
  componentId: "sc-vu58cg-1"
})(["width:", "px;height:", "px;border:1px solid ", ";background:", ";position:relative;box-sizing:border-box;flex-shrink:0;", ";"], function (p) {
  return p.theme.spacing.unit(p.size);
}, function (p) {
  return p.theme.spacing.unit(p.size);
}, function (p) {
  return p.theme.colorTokens.neutral.border_default;
}, function (p) {
  return p.theme.colorTokens.neutral.background_default;
}, function (p) {
  return p.shape === _Checkbox.Shape.Circle ? 'border-radius: 100%' : "border-radius: ".concat(p.theme.borderRadius2);
});
var StyledFormLabel = (0, _styledComponents.default)(_.FormLabel).withConfig({
  displayName: "Checkbox__StyledFormLabel",
  componentId: "sc-vu58cg-2"
})(["position:relative;&:hover ", "{border-color:", ";}"], CheckmarkBox, function (p) {
  return p.theme.colorTokens.action.border_default;
});
var Input = (0, _styledComponents.default)(CleanInput).attrs(function () {
  return {
    type: 'checkbox'
  };
}).withConfig({
  displayName: "Checkbox__Input",
  componentId: "sc-vu58cg-3"
})(["position:absolute;opacity:0;height:0;width:0;cursor:pointer;&:checked + ", "{border-color:", ";background:", ";svg{fill:", ";}}&[disabled] + ", "{border-color:", ";}&:checked[disabled] + ", "{border-color:", ";background:", ";svg{fill:", ";}}", " &:focus + ", "{border-color:", ";box-shadow:0 0 0 2px ", ";}", ""], CheckmarkBox, function (p) {
  return p.theme.colorTokens.action.background_default;
}, function (p) {
  return p.theme.colorTokens.action.background_default;
}, function (p) {
  return p.theme.colorTokens.neutral.icon_strong;
}, CheckmarkBox, function (p) {
  return p.theme.colorTokens.neutral.background_disabled;
}, CheckmarkBox, function (p) {
  return p.theme.colorTokens.neutral.background_disabled;
}, function (p) {
  return p.theme.colorTokens.neutral.background_disabled;
}, function (p) {
  return p.theme.colorTokens.neutral.icon_disabled;
}, function (p) {
  return p.hasError ? "\n    & + ".concat(CheckmarkBox, " {\n      border-color ").concat(p.theme.colorTokens.error.border_default, ";\n    }") : '';
}, CheckmarkBox, function (p) {
  return p.theme.colorTokens.action.background_default;
}, function (p) {
  return p.theme.colorTokens.action.border_focus;
}, function (p) {
  return !p.visuallyFocused ? '' : "& + ".concat(CheckmarkBox, " {\n        border-color: ").concat(p.theme.colorTokens.action.background_default, ";\n        box-shadow: 0 0 0 2px ").concat(p.theme.colorTokens.action.border_focus, ";\n      }\n  ");
});
var Label = (0, _styledComponents.default)(_.Typography).withConfig({
  displayName: "Checkbox__Label",
  componentId: "sc-vu58cg-4"
})(["padding-left:", "px;white-space:initial;"], function (p) {
  return p.theme.spacing.unit(2);
});
var components = {
  Label: Label,
  CheckmarkBox: CheckmarkBox
};
var Checkbox = function Checkbox(props) {
  var autoFocus = props.autoFocus,
    checked = props.checked,
    className = props.className,
    defaultChecked = props.defaultChecked,
    disabled = props.disabled,
    error = props.error,
    hasError = props.hasError,
    label = props.label,
    labelTooltip = props.labelTooltip,
    labelTooltipPosition = props.labelTooltipPosition,
    labelTooltipInModal = props.labelTooltipInModal,
    name = props.name,
    onBlur = props.onBlur,
    onChange = props.onChange,
    onClick = props.onClick,
    onFocus = props.onFocus,
    onKeyDown = props.onKeyDown,
    onKeyPress = props.onKeyPress,
    onKeyUp = props.onKeyUp,
    required = props.required,
    value = props.value,
    visuallyEmphasiseRequired = props.visuallyEmphasiseRequired,
    visuallyFocused = props.visuallyFocused,
    width = props.width,
    readOnly = props.readOnly,
    _props$size = props.size,
    size = _props$size === void 0 ? 'm' : _props$size,
    _props$shape = props.shape,
    shape = _props$shape === void 0 ? _Checkbox.Shape.Square : _props$shape;
  return _react.default.createElement(_.FormField, {
    error: error,
    width: width || 'auto'
  }, _react.default.createElement(StyledFormLabel, {
    className: className
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    alignItems: "center"
  }, _react.default.createElement(Input, {
    autoFocus: autoFocus,
    checked: checked,
    defaultChecked: defaultChecked,
    disabled: disabled,
    hasError: hasError || checkIfHasError(error),
    name: name,
    onBlur: onBlur,
    onChange: onChange,
    onClick: onClick,
    onFocus: onFocus,
    onKeyDown: onKeyDown,
    onKeyPress: onKeyPress,
    onKeyUp: onKeyUp,
    readOnly: readOnly,
    required: required,
    value: value,
    visuallyFocused: visuallyFocused
  }), _react.default.createElement(CheckmarkBox, {
    container: true,
    alignItems: "center",
    justifyContent: "center",
    size: getSize(size),
    shape: shape
  }, _react.default.createElement(_.OldIcon.CheckMark, {
    size: 3,
    color: "transparent"
  })), _react.default.createElement(Label, {
    type: "secondary",
    color: function color(t) {
      return disabled ? t.colorTokens.neutral.text_disabled : t.colorTokens.neutral.text_default;
    }
  }, visuallyEmphasiseRequired ? "".concat(label, " *") : label, labelTooltip && _react.default.createElement(_.Tooltip, _extends({
    label: labelTooltip
  }, labelTooltipPosition && {
    position: labelTooltipPosition
  }, {
    inModal: labelTooltipInModal,
    wrapChild: labelTooltipInModal
  }), _react.default.createElement(TooltipIcon, {
    size: 4
  }))))));
};
Checkbox.components = components;
Checkbox.Shape = _Checkbox.Shape;
var _default = exports.default = Checkbox;