"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SIZE = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _Text = _interopRequireDefault(require("../text/Text"));

var _useChipContext = _interopRequireDefault(require("./useChipContext"));

var _ChipGroup = _interopRequireDefault(require("./ChipGroup"));

var _excluded = ["children", "className", "value", "style", "name", "disabled", "size", "multiSelect", "checked", "required", "icon", "onChange", "aria-describedby"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || 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); }

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

var SIZE = {
  M: 'm',
  S: 's'
};
exports.SIZE = SIZE;

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 = (0, _classnames.default)('sg-chip', "sg-chip--".concat(size), className);
  var inputClass = (0, _classnames.default)('sg-chip__input');
  var chipGroupContext = (0, _useChipContext.default)();
  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.default, {
    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.default;
var _default = Chip;
exports.default = _default;