import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

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) { _defineProperty(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; }

import { createBEM } from '@zarm-design/bem';
import React, { createRef, forwardRef, useCallback, useContext, useState } from 'react';
import CascaderView from '../cascader-view';
import { parseItems, parseState } from '../cascader-view/utils';
import { ConfigContext } from '../config-provider';
import PickerContainer from '../picker/Container';
var Cascader = /*#__PURE__*/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__*/createRef();

  var _useState = useState(_objectSpread({}, parseState(props))),
      _useState2 = _slicedToArray(_useState, 2),
      state = _useState2[0],
      setState = _useState2[1];

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

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

  var handleConfirm = function handleConfirm() {
    var items = 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 = 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.createElement(PickerContainer, {
    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.createElement(CascaderView, {
    ref: cascaderViewRef,
    value: value,
    defaultValue: defaultValue,
    fieldNames: fieldNames,
    dataSource: dataSource,
    cols: cols,
    itemRender: itemRender,
    onChange: onValueChange
  }));
});
Cascader.displayName = 'Cascader';
Cascader.defaultProps = {
  maskClosable: true
};
export default Cascader;