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; }
import React, { useState } from 'react';
import Color from 'color';
import styled, { css } from 'styled-components';
import { Box, Card, Flexbox, Icon, Typography } from '../..';
import { isBoolean, isElement } from '../../common/utils';
var StyledFlexbox = styled(Flexbox).withConfig({
  displayName: "SelectionCard__StyledFlexbox",
  componentId: "sc-1igqsqv-0"
})(["width:100%;"]);
var StyledImg = styled.img.withConfig({
  displayName: "SelectionCard__StyledImg",
  componentId: "sc-1igqsqv-1"
})(["width:100%;"]);
var StyledInput = styled.input.withConfig({
  displayName: "SelectionCard__StyledInput",
  componentId: "sc-1igqsqv-2"
})(["opacity:0;pointer-events:none;"]);
var AbsoluteFlexbox = styled(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 = styled(Typography).withConfig({
  displayName: "SelectionCard__Tag",
  componentId: "sc-1igqsqv-4"
})(["box-sizing:border-box;", ""], function (p) {
  return "\n    padding: 0px ".concat(p.theme.spacing.unit(1), "px;\n    background: ").concat(p.theme.color.cta, ";\n    color: ").concat(p.theme.color.textLight, ";\n  ");
});
var CircleOutline = styled.div.withConfig({
  displayName: "SelectionCard__CircleOutline",
  componentId: "sc-1igqsqv-5"
})(["border-radius:100%;", ""], function (p) {
  return "\n    border: 1px solid ".concat(p.theme.color.selectionCardBorder, ";\n    height: ").concat(p.theme.spacing.unit(4), "px;\n    width: ").concat(p.theme.spacing.unit(4), "px;\n  ");
});
var outlineStyles = css(["", ";outline-offset:-2px;vertical-align:top;"], function (p) {
  return "outline: 2px solid ".concat(p.theme.color.cta);
});
var overlayStyles = css(["", ";", ""], function (p) {
  return "background: ".concat(Color(p.theme.color.cta).alpha(0.1).string());
}, outlineStyles);
var StyledCard = styled(Card).withConfig({
  displayName: "SelectionCard__StyledCard",
  componentId: "sc-1igqsqv-6"
})(["height:100%;position:relative;box-sizing:border-box;", " ", ";", ";", ";border-radius:", ";overflow:hidden;&:hover{", ";}"], function (p) {
  return "\n    cursor: ".concat(p.$disabled ? 'not-allowed' : 'pointer', ";\n    color: ").concat(p.$disabled ? p.theme.color.disabledText : p.theme.color.text, ";\n    background: ").concat(p.$disabled ? p.theme.color.disabledBackground : p.theme.color.card, ";\n  ");
}, function (p) {
  return p.$border && "border: 1px solid ".concat(p.theme.color.inputBorder);
}, function (p) {
  return p.$error && !p.$disabled && "border: 1px solid ".concat(p.theme.color.functionRed);
}, function (p) {
  return !p.$disabled && p.$selected && overlayStyles;
}, function (_ref) {
  var theme = _ref.theme;
  return theme.borderRadius8;
}, function (p) {
  return !p.$disabled && overlayStyles;
});
var StyledLabel = styled.label.withConfig({
  displayName: "SelectionCard__StyledLabel",
  componentId: "sc-1igqsqv-7"
})(["cursor:inherit;&:focus-within{", "{", "}}"], StyledCard, outlineStyles);
var StyledDiv = styled('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  ");
});
export var 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 = useState(selectedInitially),
    _useState2 = _slicedToArray(_useState, 2),
    selectedInternal = _useState2[0],
    setSelectedInternal = _useState2[1];
  var isControlled = isBoolean(controlledSelected);
  var selected = isControlled ? controlledSelected : selectedInternal;
  var changeHandler = function changeHandler(val) {
    if (typeof onChange === 'function') {
      onChange(val);
    }
    if (!isControlled) {
      setSelectedInternal(val);
    }
  };
  var titleItem = isElement(title) ? title : React.createElement(StyledFlexbox, {
    item: true
  }, React.createElement(Typography, {
    type: "primary",
    weight: "bold"
  }, title));
  var textItem = isElement(text) ? text : React.createElement(StyledFlexbox, {
    item: true
  }, React.createElement(Typography, {
    type: "secondary",
    color: function color(t) {
      return t.colorTokens.neutral.text_weak;
    }
  }, text));
  var hasIcon = Boolean(icon && !imageUrl);
  var hasFeature = Boolean(imageUrl || icon);
  return React.createElement(StyledLabel, null, React.createElement(StyledCard, {
    $disabled: disabled,
    $selected: selected,
    $border: border,
    $error: error
  }, imageUrl && React.createElement(StyledImg, {
    src: imageUrl,
    alt: imageAlt
  }), React.createElement(AbsoluteFlexbox, {
    container: true,
    justifyContent: "space-between",
    direction: "row"
  }, React.createElement(Flexbox, {
    item: true
  }, tag && React.createElement(Tag, {
    type: "secondary"
  }, tag)), React.createElement(Box, {
    pr: 2
  }, !disabled && !selected && outline && React.createElement(CircleOutline, null), !disabled && selected && React.createElement(Icon.CheckCircleFill16, {
    color: function color(t) {
      return t.color.cta;
    }
  }), React.createElement(StyledInput, {
    type: "checkbox",
    disabled: disabled,
    checked: selected,
    onChange: function onChange() {
      return changeHandler(!selected);
    }
  }))), React.createElement(StyledDiv, {
    $feature: hasFeature,
    $tag: Boolean(tag),
    $text: Boolean(text),
    $textAlignLeft: alignLeft
  }, horizontal && React.createElement(Flexbox, {
    container: true,
    direction: "row",
    gap: 5,
    alignContent: "center"
  }, hasIcon && React.createElement(Flexbox, {
    item: true,
    alignSelf: "center"
  }, icon), React.createElement(Flexbox, {
    container: true,
    direction: "column",
    gap: 1,
    alignItems: "flex-start"
  }, titleItem, textItem)), !horizontal && React.createElement(Flexbox, {
    container: true,
    direction: "column",
    alignItems: alignLeft ? 'flex-start' : 'center',
    gap: 3
  }, hasIcon && React.createElement(Flexbox, _extends({
    item: true
  }, text && {
    alignSelf: 'flex-start'
  }), icon), React.createElement(Flexbox, _extends({
    item: true,
    container: true,
    direction: "column"
  }, text && {
    alignSelf: 'flex-start'
  }, {
    width: "100%"
  }), titleItem, textItem)))));
};