"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = exports.accordionSectionItemBuilder = exports.accordionItemBuilder = exports.Accordion = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _AccordionSt = require("./Accordion.st.css.js");
var _AccordionItem = _interopRequireDefault(require("./components/AccordionItem"));
var _constants = require("./constants");
var _SectionHeader = _interopRequireDefault(require("../SectionHeader"));
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Accordion/Accordion.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
class Accordion extends _react.PureComponent {
  constructor(props) {
    super(props);
    this._findOpenIndexes = function () {
      var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var initial = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
      return items.map((item, index) => initial && item.initiallyOpen || item.open ? index : null).filter(index => index !== null).flatMap(index => !!index || index === 0 ? [index] : []);
    };
    this._compareOpenItems = function () {
      var currentItems = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var prevItems = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
      if (prevItems.length !== currentItems.length) {
        return false;
      }
      for (var i = 0; i < prevItems.length; i++) {
        if (prevItems[i].open !== currentItems[i].open) {
          return false;
        }
      }
      return true;
    };
    this.componentDidUpdate = prevProps => {
      if (!this._compareOpenItems(this.props.items, prevProps.items)) {
        this.setState({
          openIndexes: this._findOpenIndexes(this.props.items)
        });
      }
    };
    this._toggle = index => () => this.setState(_ref => {
      var {
        openIndexes
      } = _ref;
      return {
        openIndexes: openIndexes.includes(index) ? openIndexes.filter(i => i !== index) : this.props.multiple ? [...openIndexes, index] : [index]
      };
    });
    this.state = {
      openIndexes: this._findOpenIndexes(this.props.items, true)
    };
  }
  render() {
    var {
      openIndexes
    } = this.state;
    var {
      dataHook,
      items,
      skin,
      hideShadow,
      size,
      horizontalPadding,
      contentPadding,
      transitionSpeed,
      onAnimationEnter,
      onAnimationExit,
      titleSize
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-hook": dataHook,
      className: _AccordionSt.classes.accordion,
      "data-transition-speed": transitionSpeed,
      "data-horizontal-padding": horizontalPadding,
      "data-content-padding": contentPadding,
      "data-skin": skin,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 94,
        columnNumber: 7
      }
    }, items == null ? void 0 : items.map((item, index, allItems) => {
      var uncontrolledProps = {
        onToggle: this._toggle(index),
        open: openIndexes.includes(index),
        onAnimationEnter,
        onAnimationExit
      };
      var last = index === allItems.length - 1;
      var internalProps = {
        className: (0, _AccordionSt.st)(_AccordionSt.classes.item, {
          last
        }),
        key: item.key || index,
        skin,
        hideShadow,
        size,
        horizontalPadding,
        contentPadding,
        last,
        transitionSpeed,
        titleSize
      };
      if (typeof item.render === 'function') {
        return item.render(uncontrolledProps, internalProps);
      } else {
        return /*#__PURE__*/_react.default.createElement(_AccordionItem.default, (0, _extends2.default)({}, uncontrolledProps, item, internalProps, {
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 128,
            columnNumber: 15
          }
        }));
      }
    }));
  }
}
exports.Accordion = Accordion;
Accordion.displayName = 'Accordion';
Accordion.defaultProps = {
  items: [],
  multiple: false,
  skin: _constants.skins.standard,
  horizontalPadding: _constants.horizontalPaddings.large,
  hideShadow: false,
  transitionSpeed: 'medium',
  size: 'small',
  titleSize: 'medium'
};
var accordionItemBuilder = item => {
  return _objectSpread(_objectSpread({}, item), {}, {
    render: (uncontrolledProps, internalProps) => /*#__PURE__*/_react.default.createElement(_AccordionItem.default, (0, _extends2.default)({}, uncontrolledProps, item, internalProps, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 147,
        columnNumber: 10
      }
    }))
  });
};
exports.accordionItemBuilder = accordionItemBuilder;
var accordionSectionItemBuilder = item => {
  return _objectSpread(_objectSpread({}, item), {}, {
    render: (_, internalProps) => {
      var _item$title;
      return /*#__PURE__*/_react.default.createElement(_SectionHeader.default, {
        title: (_item$title = item.title) !== null && _item$title !== void 0 ? _item$title : internalProps.title,
        skin: "neutral",
        __self: void 0,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 160,
          columnNumber: 7
        }
      });
    }
  });
};
exports.accordionSectionItemBuilder = accordionSectionItemBuilder;
var _default = exports.default = Accordion;
//# sourceMappingURL=Accordion.js.map