"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SelectionCard = void 0;
var _react = _interopRequireWildcard(require("react"));
var _color = _interopRequireDefault(require("color"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _ = require("../..");
var _utils = require("../../common/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var StyledFlexbox = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "SelectionCard__StyledFlexbox",
  componentId: "sc-1igqsqv-0"
})(["width:100%;"]);
var StyledImg = _styledComponents.default.img.withConfig({
  displayName: "SelectionCard__StyledImg",
  componentId: "sc-1igqsqv-1"
})(["width:100%;"]);
var StyledInput = _styledComponents.default.input.withConfig({
  displayName: "SelectionCard__StyledInput",
  componentId: "sc-1igqsqv-2"
})(["opacity:0;pointer-events:none;"]);
var AbsoluteFlexbox = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "SelectionCard__AbsoluteFlexbox",
  componentId: "sc-1igqsqv-3"
})(["color:inherit;width:100%;position:absolute;", ";"], function (p) {
  return "top: ".concat(p.theme.spacing.unit(3), "px");
});
var Tag = (0, _styledComponents.default)(_.Typography).withConfig({
  displayName: "SelectionCard__Tag",
  componentId: "sc-1igqsqv-4"
})(["box-sizing:border-box;", ""], function (p) {
  return "\n    padding: ".concat(p.theme.spacing.unit(1), "px;\n    background: ").concat(p.theme.colorTokens.action.background_default, ";\n    color: ").concat(p.theme.colorTokens.neutral.text_strong, ";\n  ");
});
var CircleOutline = _styledComponents.default.div.withConfig({
  displayName: "SelectionCard__CircleOutline",
  componentId: "sc-1igqsqv-5"
})(["border-radius:100%;", ""], function (p) {
  return "\n    border: 1px solid ".concat(p.theme.colorTokens.neutral.border_default, ";\n    height: ").concat(p.theme.spacing.unit(4), "px;\n    width: ").concat(p.theme.spacing.unit(4), "px;\n  ");
});
var outlineStyles = (0, _styledComponents.css)(["", ";", ";vertical-align:top;"], function (p) {
  return "outline-color: ".concat(p.theme.colorTokens.action.border_default);
}, function (p) {
  return "border-color: ".concat(p.theme.colorTokens.action.border_default);
});
var overlayStyles = (0, _styledComponents.css)(["", ";", ""], function (p) {
  return "background: ".concat((0, _color.default)(p.theme.colorTokens.action.background_weak).string());
}, outlineStyles);
var StyledCard = (0, _styledComponents.default)(_.Card).withConfig({
  displayName: "SelectionCard__StyledCard",
  componentId: "sc-1igqsqv-6"
})(["height:100%;position:relative;box-sizing:border-box;", " ", ";", ";border-radius:", ";overflow:hidden;outline-offset:-2px;", ";transition-duration:0.2s;transition-timing-function:ease;transition-property:background-color,outline-color,border-color;&:hover{", ";}"], function (p) {
  return "\n    cursor: ".concat(p.$disabled ? 'not-allowed' : 'pointer', ";\n    color: ").concat(p.$disabled ? p.theme.colorTokens.neutral.text_disabled : p.theme.colorTokens.neutral.text_default, ";\n    background: ").concat(p.$disabled ? p.theme.colorTokens.neutral.background_disabled : p.theme.colorTokens.neutral.background_default, ";\n    outline: ").concat(p.$border ? "1px solid transparent" : "2px solid transparent", ";\n    ");
}, function (p) {
  return p.$error && !p.$disabled && "border: 1px solid ".concat(p.theme.colorTokens.error.border_default);
}, function (p) {
  return !p.$disabled && p.$selected && overlayStyles;
}, function (_ref) {
  var theme = _ref.theme;
  return theme.borderRadius8;
}, function (p) {
  return p.$border && "border: 1px solid ".concat(p.theme.colorTokens.neutral.border_default);
}, function (p) {
  return !p.$disabled && overlayStyles;
});
var StyledLabel = _styledComponents.default.label.withConfig({
  displayName: "SelectionCard__StyledLabel",
  componentId: "sc-1igqsqv-7"
})(["cursor:inherit;&:focus-within{", "{", "}}"], StyledCard, outlineStyles);
var StyledDiv = (0, _styledComponents.default)('div').withConfig({
  displayName: "SelectionCard__StyledDiv",
  componentId: "sc-1igqsqv-8"
})(["", " ", " ", ""], function (p) {
  return "\n    text-align: ".concat(p.$text || p.$textAlignLeft ? 'left' : 'center', ";\n    padding: ").concat(p.theme.spacing.unit(3), "px;\n  ");
}, function (p) {
  return p.$tag && p.$text && "padding-top: ".concat(p.theme.spacing.unit(7), "px;\n      padding-bottom: ").concat(p.theme.spacing.unit(5), "px;\n  ");
}, function (p) {
  return p.$tag && !p.$feature && "padding-top:".concat(p.theme.spacing.unit(10), "px;\n      padding-bottom:").concat(p.theme.spacing.unit(10), "px;\n  ");
});
var StyledItem = (0, _styledComponents.default)(_.Typography).withConfig({
  displayName: "SelectionCard__StyledItem",
  componentId: "sc-1igqsqv-9"
})(["", ""], function (p) {
  return "\n    color: ".concat(p.$disabled ? p.theme.colorTokens.neutral.text_disabled : '', ";\n  ");
});
var SelectionCard = exports.SelectionCard = function SelectionCard(_ref2) {
  var title = _ref2.title,
    _ref2$onChange = _ref2.onChange,
    onChange = _ref2$onChange === void 0 ? function () {} : _ref2$onChange,
    _ref2$tag = _ref2.tag,
    tag = _ref2$tag === void 0 ? '' : _ref2$tag,
    _ref2$text = _ref2.text,
    text = _ref2$text === void 0 ? '' : _ref2$text,
    _ref2$imageUrl = _ref2.imageUrl,
    imageUrl = _ref2$imageUrl === void 0 ? '' : _ref2$imageUrl,
    _ref2$imageAlt = _ref2.imageAlt,
    imageAlt = _ref2$imageAlt === void 0 ? '' : _ref2$imageAlt,
    _ref2$icon = _ref2.icon,
    icon = _ref2$icon === void 0 ? null : _ref2$icon,
    _ref2$border = _ref2.border,
    border = _ref2$border === void 0 ? false : _ref2$border,
    _ref2$disabled = _ref2.disabled,
    disabled = _ref2$disabled === void 0 ? false : _ref2$disabled,
    _ref2$error = _ref2.error,
    error = _ref2$error === void 0 ? false : _ref2$error,
    _ref2$horizontal = _ref2.horizontal,
    horizontal = _ref2$horizontal === void 0 ? false : _ref2$horizontal,
    _ref2$outline = _ref2.outline,
    outline = _ref2$outline === void 0 ? false : _ref2$outline,
    controlledSelected = _ref2.selected,
    _ref2$selectedInitial = _ref2.selectedInitially,
    selectedInitially = _ref2$selectedInitial === void 0 ? false : _ref2$selectedInitial,
    _ref2$alignLeft = _ref2.alignLeft,
    alignLeft = _ref2$alignLeft === void 0 ? false : _ref2$alignLeft;
  var _useState = (0, _react.useState)(selectedInitially),
    _useState2 = _slicedToArray(_useState, 2),
    selectedInternal = _useState2[0],
    setSelectedInternal = _useState2[1];
  var isControlled = (0, _utils.isBoolean)(controlledSelected);
  var selected = isControlled ? controlledSelected : selectedInternal;
  var changeHandler = function changeHandler(val) {
    if (typeof onChange === 'function') {
      onChange(val);
    }
    if (!isControlled) {
      setSelectedInternal(val);
    }
  };
  var titleItem = (0, _utils.isElement)(title) ? title : _react.default.createElement(StyledFlexbox, {
    item: true
  }, _react.default.createElement(StyledItem, {
    type: "primary",
    weight: "bold",
    $disabled: disabled
  }, title));
  var textItem = (0, _utils.isElement)(text) ? text : _react.default.createElement(StyledFlexbox, {
    item: true
  }, _react.default.createElement(StyledItem, {
    type: "secondary",
    color: function color(t) {
      return t.colorTokens.neutral.text_weak;
    },
    $disabled: disabled
  }, text));
  var hasIcon = Boolean(icon && !imageUrl);
  var hasFeature = Boolean(imageUrl || icon);
  return _react.default.createElement(StyledLabel, null, _react.default.createElement(StyledCard, {
    $disabled: disabled,
    $selected: selected,
    $border: border,
    $error: error
  }, imageUrl && _react.default.createElement(StyledImg, {
    src: imageUrl,
    alt: imageAlt
  }), _react.default.createElement(AbsoluteFlexbox, {
    container: true,
    justifyContent: "space-between",
    direction: "row"
  }, _react.default.createElement(_.Flexbox, {
    item: true
  }, tag && _react.default.createElement(Tag, {
    type: "secondary"
  }, tag)), _react.default.createElement(_.Box, {
    pr: 2
  }, !disabled && !selected && outline && _react.default.createElement(CircleOutline, null), !disabled && selected && _react.default.createElement(_.Icon.CheckCircleFill16, {
    color: function color(t) {
      return t.colorTokens.action.background_default;
    }
  }), _react.default.createElement(StyledInput, {
    type: "checkbox",
    disabled: disabled,
    checked: selected,
    onChange: function onChange() {
      return changeHandler(!selected);
    }
  }))), _react.default.createElement(StyledDiv, {
    $feature: hasFeature,
    $tag: Boolean(tag),
    $text: Boolean(text),
    $textAlignLeft: alignLeft
  }, horizontal && _react.default.createElement(_.Flexbox, {
    container: true,
    direction: "row",
    gap: 5,
    alignContent: "center"
  }, hasIcon && _react.default.createElement(_.Flexbox, {
    item: true,
    alignSelf: "center"
  }, icon), _react.default.createElement(_.Flexbox, {
    container: true,
    direction: "column",
    gap: 1,
    alignItems: "flex-start"
  }, titleItem, textItem)), !horizontal && _react.default.createElement(_.Flexbox, {
    container: true,
    direction: "column",
    alignItems: alignLeft ? 'flex-start' : 'center',
    gap: 3
  }, hasIcon && _react.default.createElement(_.Flexbox, _extends({
    item: true
  }, text && {
    alignSelf: 'flex-start'
  }), icon), _react.default.createElement(_.Flexbox, _extends({
    item: true,
    container: true,
    direction: "column"
  }, text && {
    alignSelf: 'flex-start'
  }, {
    width: "100%"
  }), titleItem, textItem)))));
};