import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["children", "className", "value", "style", "name", "disabled", "size", "multiSelect", "checked", "required", "icon", "onChange", "aria-describedby"];
import * as React from 'react';
import classNames from 'classnames';
import Text from '../text/Text';
import useChipContext from './useChipContext';
import ChipGroup from './ChipGroup';
export var SIZE = {
  M: 'm',
  S: 's'
};

var isCheckedWithinGroup = function isCheckedWithinGroup(groupValue, value) {
  if (!value) {
    return;
  }

  if (Array.isArray(groupValue)) {
    return groupValue.includes(value);
  }

  return groupValue === value;
};

var Chip = function Chip(_ref) {
  var children = _ref.children,
      className = _ref.className,
      value = _ref.value,
      style = _ref.style,
      name = _ref.name,
      disabled = _ref.disabled,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'm' : _ref$size,
      multiSelect = _ref.multiSelect,
      checked = _ref.checked,
      required = _ref.required,
      icon = _ref.icon,
      onChange = _ref.onChange,
      ariaDescribedBy = _ref['aria-describedby'],
      props = _objectWithoutProperties(_ref, _excluded);

  var chipClass = classNames('sg-chip', "sg-chip--".concat(size), className);
  var inputClass = classNames('sg-chip__input');
  var chipGroupContext = useChipContext();
  var isWithinChipGroup = Boolean(chipGroupContext && Object.keys(chipGroupContext).length);
  var isCheckbox = multiSelect || chipGroupContext.multiSelect;
  var inputType = isCheckbox ? 'checkbox' : 'radio';
  var groupName = isWithinChipGroup ? chipGroupContext.name : name;
  var isRequired = isWithinChipGroup ? chipGroupContext.required : required;
  var isDisabled = isWithinChipGroup ? chipGroupContext.disabled : disabled;
  var isControlled = checked !== undefined || isWithinChipGroup;
  var isChecked = isControlled ? checked !== null && checked !== void 0 ? checked : isCheckedWithinGroup(chipGroupContext.groupValue, value) : undefined;

  var onInputChange = function onInputChange(e) {
    if (isWithinChipGroup) {
      chipGroupContext.onChipChange(e, value);
    }

    if (onChange) {
      onChange(e);
    }
  };

  return /*#__PURE__*/React.createElement("label", _extends({
    className: chipClass
  }, props, {
    style: style // On iOS the :active pseudo state is triggered only when there is a touch event set on the HTML element
    // and we use active pseudo class to provide press feedback.
    ,
    onTouchStart: function onTouchStart() {
      return null;
    }
  }), /*#__PURE__*/React.createElement("input", {
    className: inputClass,
    type: inputType,
    disabled: isDisabled,
    required: isRequired,
    value: value,
    checked: isChecked,
    "aria-describedby": ariaDescribedBy,
    onChange: onInputChange,
    name: groupName
  }), /*#__PURE__*/React.createElement("div", {
    className: "sg-chip__pill"
  }, Boolean(icon) && /*#__PURE__*/React.createElement("div", {
    className: "sg-chip__icon"
  }, icon), /*#__PURE__*/React.createElement(Text, {
    color: "text-black",
    size: "small",
    weight: "bold",
    className: "sg-chip__text"
  }, children), isCheckbox && /*#__PURE__*/React.createElement("svg", {
    viewBox: "0 0 16 16",
    xmlns: "http://www.w3.org/2000/svg",
    className: "sg-chip__check-icon"
  }, /*#__PURE__*/React.createElement("line", {
    x1: "3.5",
    y1: "8",
    x2: "12.5",
    y2: "8",
    stroke: "currentColor",
    strokeWidth: "2",
    strokeLinecap: "round",
    className: "sg-chip__line sg-chip__line--horizontal"
  }), /*#__PURE__*/React.createElement("line", {
    y2: "3.5",
    x2: "8",
    y1: "12.5",
    x1: "8",
    stroke: "currentColor",
    strokeWidth: "2",
    strokeLinecap: "round",
    className: "sg-chip__line sg-chip__line--vertical"
  }))));
};

Chip.Group = ChipGroup;
export default Chip;