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 styled from 'styled-components';
import { AnimatePresence, motion } from 'framer-motion';
import { Box, Typography, Flexbox, Icon } from '../..';
import { isBoolean, isFunction, isString } from '../../common/utils';
var TRANSITION_DURATION = 0.16;
var Item = styled.div.withConfig({
  displayName: "AccordionItem__Item",
  componentId: "sc-1tey38x-0"
})(["outline:1px ", ";border-radius:", ";background-color:", ";&& + &{border-top:1px solid ", ";}", ""], function (p) {
  return !p.disableFocusOutline && p.hasFocus ? "solid ".concat(p.theme.color.cta) : 'none';
}, function (p) {
  return p.theme.borderRadius4;
}, function (p) {
  return !p.disableBackgroundColor && p.hasFocus ? p.theme.color.background : 'transparent';
}, function (p) {
  return p.theme.color.divider;
}, function (_ref) {
  var theme = _ref.theme,
    p = _ref.p,
    px = _ref.px,
    py = _ref.py,
    pt = _ref.pt,
    pb = _ref.pb,
    pl = _ref.pl,
    pr = _ref.pr;
  return "\n  padding: ".concat(theme.spacing.unit(pt || py || p || 0), "px ").concat(theme.spacing.unit(pr || px || p || 0), "px\n    ").concat(theme.spacing.unit(pb || py || p || 0), "px ").concat(theme.spacing.unit(pl || px || p || 0), "px;\n  ");
});
var Button = styled.button.withConfig({
  displayName: "AccordionItem__Button",
  componentId: "sc-1tey38x-1"
})(["font:inherit;width:100%;border:0;display:flex;text-align:start;position:relative;align-items:center;box-sizing:border-box;background-color:transparent;padding:", "px 0;cursor:", ";color:", ";justify-content:", ";"], function (p) {
  return p.theme.spacing.unit(3);
}, function (p) {
  return p.disabled ? 'default' : 'pointer';
}, function (_ref2) {
  var disabled = _ref2.disabled,
    theme = _ref2.theme;
  return disabled ? theme.color.disabledText : '';
}, function (_ref3) {
  var addonOnRightSide = _ref3.addonOnRightSide;
  return addonOnRightSide ? 'space-between' : 'flex-start';
});
var IconWrapper = styled.div.withConfig({
  displayName: "AccordionItem__IconWrapper",
  componentId: "sc-1tey38x-2"
})(["", " svg{transform:rotate(", "deg);transition:transform ", "s ease-out;}"], function (_ref4) {
  var withChevron = _ref4.withChevron,
    theme = _ref4.theme;
  return "\n      margin-top: ".concat(withChevron ? '0' : '-2px', ";\n      order: ").concat(withChevron ? '1' : '-1', ";\n      padding-right: ").concat(withChevron ? 0 : theme.spacing.unit(3), "px;\n    ");
}, function (p) {
  return p.$expanded ? '0' : '180';
}, TRANSITION_DURATION);
var LeftAddonWrapper = styled.div.withConfig({
  displayName: "AccordionItem__LeftAddonWrapper",
  componentId: "sc-1tey38x-3"
})(["", ""], function (_ref5) {
  var theme = _ref5.theme;
  return "\n     \n      padding-right: ".concat(theme.spacing.unit(2), "px;\n    ");
});
var StyledButton = styled(Button).withConfig({
  displayName: "AccordionItem__StyledButton",
  componentId: "sc-1tey38x-4"
})(["-webkit-tap-highlight-color:transparent;"]);
var Content = styled(Box).withConfig({
  displayName: "AccordionItem__Content",
  componentId: "sc-1tey38x-5"
})([""]);
var components = {
  Content: Content
};
export var AccordionItem = React.forwardRef(function (_ref6, ref) {
  var _ref6$as = _ref6.as,
    as = _ref6$as === void 0 ? 'h3' : _ref6$as,
    children = _ref6.children,
    className = _ref6.className,
    controlledExpand = _ref6.expanded,
    expandedInitial = _ref6.expandedInitial,
    title = _ref6.title,
    label = _ref6.label,
    onClick = _ref6.onClick,
    onToggle = _ref6.onToggle,
    withChevron = _ref6.withChevron,
    disableBackgroundColor = _ref6.disableBackgroundColor,
    disableFocusOutline = _ref6.disableFocusOutline,
    disabled = _ref6.disabled,
    p = _ref6.p,
    px = _ref6.px,
    py = _ref6.py,
    pt = _ref6.pt,
    pb = _ref6.pb,
    pl = _ref6.pl,
    pr = _ref6.pr,
    leftBadgeIcon = _ref6.leftBadgeIcon,
    rightAddon = _ref6.rightAddon,
    _ref6$type = _ref6.type,
    type = _ref6$type === void 0 ? 'secondary' : _ref6$type;
  var _useState = useState(expandedInitial || false),
    _useState2 = _slicedToArray(_useState, 2),
    expandedInternal = _useState2[0],
    setExpandedInternal = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    hasFocus = _useState4[0],
    setHasFocus = _useState4[1];
  var isControlled = isBoolean(controlledExpand);
  var expanded = isControlled ? controlledExpand : expandedInternal;
  var clickHandler = function clickHandler(e) {
    if (!isControlled) {
      setExpandedInternal(!expandedInternal);
    }
    if (isFunction(onClick)) {
      onClick(e);
    }
    if (isFunction(onToggle)) {
      onToggle(!expanded);
    }
  };
  var icon = function () {
    if (withChevron && type === 'primary') return React.createElement(Icon.ChevronUp16, {
      color: function color(t) {
        return disabled ? t.color.disabledText : t.color.svgFill;
      }
    });
    if (withChevron) return React.createElement(Icon.ChevronUp8, {
      color: function color(t) {
        return disabled ? t.color.disabledText : t.color.svgFill;
      }
    });
    if (expanded) return React.createElement(Icon.Subtract16, {
      color: function color(t) {
        return t.color.cta;
      }
    });
    return React.createElement(Icon.Add16, {
      color: function color(t) {
        return disabled ? t.color.disabledText : t.color.cta;
      }
    });
  }();
  var hasLeftBadgeIcon = React.isValidElement(leftBadgeIcon);
  var hasRightAddon = React.isValidElement(rightAddon);
  var padding = {
    p: p,
    px: px,
    py: py,
    pt: pt,
    pb: pb,
    pl: pl,
    pr: pr
  };
  var chevronPadding = withChevron ? 0 : 6;
  var contentPadding = leftBadgeIcon ? 10 : chevronPadding;
  return React.createElement(Item, _extends({
    className: className,
    "aria-expanded": expanded,
    hasFocus: hasFocus,
    disableBackgroundColor: disableBackgroundColor,
    disableFocusOutline: disableFocusOutline
  }, padding), React.createElement(Typography, {
    as: as,
    type: type,
    weight: "bold"
  }, React.createElement(StyledButton, {
    ref: ref,
    onClick: clickHandler,
    type: "button",
    onBlur: function onBlur() {
      return setHasFocus(false);
    },
    onFocus: function onFocus() {
      return setHasFocus(true);
    },
    disabled: disabled,
    addonOnRightSide: hasRightAddon || withChevron
  }, React.createElement(Flexbox, {
    container: true,
    justifyContent: "flex-start",
    alignItems: "center"
  }, hasLeftBadgeIcon && React.createElement(LeftAddonWrapper, null, leftBadgeIcon), React.createElement(Flexbox, {
    container: true,
    item: true,
    direction: "column"
  }, React.createElement(Typography, {
    type: type,
    weight: "bold"
  }, title), React.createElement(Typography, {
    type: "tertiary"
  }, label))), hasRightAddon ? rightAddon : React.createElement(IconWrapper, {
    withChevron: withChevron,
    $expanded: expanded
  }, icon))), React.createElement(AnimatePresence, {
    initial: false
  }, expanded && React.createElement(motion.section, {
    initial: "collapsed",
    animate: "open",
    exit: "collapsed",
    variants: {
      open: {
        opacity: 1,
        height: 'auto'
      },
      collapsed: {
        opacity: 0,
        height: 0
      }
    },
    transition: {
      duration: TRANSITION_DURATION,
      ease: 'easeOut'
    }
  }, React.createElement(Content, {
    pt: 1,
    pb: 3,
    pl: contentPadding,
    pr: chevronPadding
  }, isString(children) ? React.createElement(Typography, {
    as: "p",
    type: type,
    color: function color(t) {
      return t.color.accordionText;
    }
  }, children) : children))));
});
AccordionItem.components = components;