"use strict";

exports.__esModule = true;
exports.default = exports.SideBarContext = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _hooks = require("../utils/hooks");
var _view = _interopRequireDefault(require("../view"));
var _item = _interopRequireDefault(require("./item"));
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
var SideBarContext = /*#__PURE__*/(0, _react.createContext)({
  activeKey: -1,
  align: 'left',
  change: function change() {}
});
exports.SideBarContext = SideBarContext;
var getInitActiveKey = function (props) {
  if ('activeKey' in props && props.activeKey) {
    return props.activeKey;
  } else if (props.defaultActiveKey || props.defaultActiveKey === 0) {
    return props.defaultActiveKey;
  } else {
    return undefined;
  }
};
var SideBar = function (props, ref) {
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    _props$align = props.align,
    align = _props$align === void 0 ? 'left' : _props$align,
    activeKey = props.activeKey,
    _props$defaultActiveK = props.defaultActiveKey,
    defaultActiveKey = _props$defaultActiveK === void 0 ? -1 : _props$defaultActiveK,
    _props$onChange = props.onChange,
    onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
    _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
    height = props.height,
    width = props.width,
    className = props.className,
    _props$renderContent = props.renderContent,
    renderContent = _props$renderContent === void 0 ? null : _props$renderContent,
    contentClassName = props.contentClassName,
    contentStyle = props.contentStyle,
    renderSideBar = props.renderSideBar,
    children = props.children,
    others = (0, _tslib.__rest)(props, ["prefix", "align", "activeKey", "defaultActiveKey", "onChange", "dataSource", "height", "width", "className", "renderContent", "contentClassName", "contentStyle", "renderSideBar", "children"]);
  var _useState = (0, _react.useState)(getInitActiveKey(props)),
    curActiveKey = _useState[0],
    setActiveKey = _useState[1];
  var curSideBarScrollId = (0, _hooks.useGuid)('side-bar-scroll-');
  var curSideBarScrollContainerId = (0, _hooks.useGuid)('side-bar-ctn-');
  var activeBarRef = (0, _react.useRef)();
  var isControlled = ('activeKey' in props);
  var clsPrefix = prefix + "side-bar";
  var clses = {
    container: (0, _classnames.default)([clsPrefix + "-container"]),
    bar: (0, _classnames.default)([clsPrefix, className]),
    scrollContainer: clsPrefix + "-scroll-container",
    scrollContent: clsPrefix + "-scroll-content",
    content: (0, _classnames.default)(clsPrefix + "-content", contentClassName)
  };
  (0, _react.useEffect)(function () {
    if (isControlled && curActiveKey && !props.activeKey) {
      setActiveKey(getInitActiveKey(props));
      return;
    }
    if (isControlled) {
      setActiveKey(props.activeKey);
    }
  });
  var content;
  if (dataSource && dataSource.length > 0) {
    content = dataSource.map(function (item, index) {
      return /*#__PURE__*/(0, _react.createElement)(_item.default, _extends({
        key: item.key || index,
        itemKey: item.key,
        renderContent: renderSideBar,
        ref: item.key === curActiveKey ? activeBarRef : null,
        prefix: prefix
      }, item));
    });
  } else {
    content = _react.Children.map(children, function (child, index) {
      return (0, _react.cloneElement)(child, {
        key: "mt_side_bar_" + index,
        itemKey: child.key || index,
        ref: "" + child.key === "" + curActiveKey ? activeBarRef : null,
        renderContent: child.renderContent || renderSideBar,
        prefix: prefix
      });
    });
  }
  return /*#__PURE__*/(0, _react.createElement)(SideBarContext.Provider, {
    value: {
      activeKey: curActiveKey,
      align: align,
      change: function handleChange(v) {
        if (!isControlled) {
          setActiveKey(v);
        }
        onChange(v);
      }
    }
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clses.container
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({}, others, {
    className: clses.bar,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)({
      width: width,
      maxWidth: width
    }),
    ref: ref
  }), /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.ScrollView, {
    id: curSideBarScrollId,
    className: clses.scrollContainer,
    showsVerticalScrollIndicator: false,
    showsHorizontalScrollIndicator: false,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)({
      height: height,
      width: width,
      maxWidth: width
    })
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    id: curSideBarScrollContainerId,
    className: clses.scrollContent
  }, content))), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clses.content,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(contentStyle)
  }, renderContent || function getChildren(activeBarKey) {
    var ret = null;
    _react.Children.forEach(children, function (child) {
      if (ret) {
        return;
      }
      var _key = child.key;
      if ("" + _key === "" + activeBarKey) {
        ret = child.props.children;
      }
    });
    return ret;
  }(curActiveKey))));
};
SideBar.displayName = 'SideBar';
var _default = /*#__PURE__*/(0, _react.forwardRef)(SideBar);
exports.default = _default;