"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _bem = require("@zarm-design/bem");

var _react = _interopRequireWildcard(require("react"));

var _cascaderView = _interopRequireDefault(require("../cascader-view"));

var _utils = require("../cascader-view/utils");

var _configProvider = require("../config-provider");

var _Container = _interopRequireDefault(require("../picker/Container"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var Cascader = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var className = props.className,
      style = props.style,
      title = props.title,
      visible = props.visible,
      value = props.value,
      defaultValue = props.defaultValue,
      fieldNames = props.fieldNames,
      dataSource = props.dataSource,
      cols = props.cols,
      itemRender = props.itemRender,
      mountContainer = props.mountContainer,
      safeArea = props.safeArea,
      maskClosable = props.maskClosable,
      confirmText = props.confirmText,
      cancelText = props.cancelText,
      onConfirm = props.onConfirm,
      onCancel = props.onCancel,
      onChange = props.onChange,
      afterOpen = props.afterOpen,
      afterClose = props.afterClose;
  var cascaderViewRef = /*#__PURE__*/(0, _react.createRef)();

  var _useState = (0, _react.useState)(_objectSpread({}, (0, _utils.parseState)(props))),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      state = _useState2[0],
      setState = _useState2[1];

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls,
      locale = _useContext.locale;

  var bem = (0, _bem.createBEM)('cascader', {
    prefixCls: prefixCls
  });

  var handleConfirm = function handleConfirm() {
    var items = (0, _utils.parseItems)(dataSource, state.value, fieldNames);
    onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(state.value, items);
  };

  var handleCancel = function handleCancel() {
    onCancel === null || onCancel === void 0 ? void 0 : onCancel();
  };

  var onValueChange = (0, _react.useCallback)(function (newValue, items) {
    setState(function (prevState) {
      return _objectSpread(_objectSpread({}, prevState), {}, {
        value: newValue
      });
    });
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue, items);
  }, [onChange]);
  return /*#__PURE__*/_react.default.createElement(_Container.default, {
    ref: ref,
    className: bem([className]),
    style: style,
    title: title || (locale === null || locale === void 0 ? void 0 : locale.Cascader.title),
    confirmText: confirmText || (locale === null || locale === void 0 ? void 0 : locale.Cascader.confirmText),
    cancelText: cancelText || (locale === null || locale === void 0 ? void 0 : locale.Cascader.cancelText),
    visible: visible,
    maskClosable: maskClosable,
    mountContainer: mountContainer,
    safeArea: safeArea,
    onConfirm: handleConfirm,
    onCancel: handleCancel,
    onClose: handleCancel,
    afterOpen: afterOpen,
    afterClose: afterClose
  }, /*#__PURE__*/_react.default.createElement(_cascaderView.default, {
    ref: cascaderViewRef,
    value: value,
    defaultValue: defaultValue,
    fieldNames: fieldNames,
    dataSource: dataSource,
    cols: cols,
    itemRender: itemRender,
    onChange: onValueChange
  }));
});
Cascader.displayName = 'Cascader';
Cascader.defaultProps = {
  maskClosable: true
};
var _default = Cascader;
exports.default = _default;