import { _ as _extends } from '../_rollupPluginBabelHelpers-736b301a.js';
import React__default from 'react';
import TreeView from '../Tree/index.js';
import { inject, observer } from 'mobx-react';
import TreeTitle from '../TreeTitle/index.js';
import IconFont from '../IconFont/index.js';
import { thousand, isEqual, treeHelper } from '@zenview/micro-utils';
import '../index-71189681.js';
import '../_commonjsHelpers-e7b0df42.js';
import 'react-is';
import 'antd';
import '../HightLevel/index.js';
import 'lodash';
import '../SearchInput/index.js';

var _dec, _class;
let PlaceTree = (_dec = inject('place'), _dec(_class = observer(_class = class PlaceTree extends React__default.Component {
  constructor(props) {
    super(props);
    this.onKeywordChange = value => {
      if (!this.props.keyword) {
        this.onChange(value);
      }
    };
    this.onChange = value => {
      const {
        place
      } = this.props;
      let expandedKeys = [];
      if (value) {
        let keys = [];
        const placeList = place.placeList.filter(v => v.name.indexOf(value) > -1);
        const placeCodes = [];
        placeList.forEach(v => {
          if (v.pcodes) {
            placeCodes.push(...v.pcodes);
          }
          placeCodes.push(v.areaCode);
        });
        keys = [...new Set([...keys])];
        placeCodes.forEach(v => {
          let codes = place.getPlaceCodesWithParent(v);
          keys.push(...codes);
        });
        expandedKeys = [...new Set([...keys])];
      }
      this.setState({
        expandedKeys,
        keyword: value
      });
    };
    this.onExpand = expandedKeys => {
      this.props.onExpand && this.props.onExpand(expandedKeys);
      this.setState({
        expandedKeys
      });
    };
    this.renderSuffix = node => {
      const {
        showCount,
        renderSuffix
      } = this.props;
      if (!renderSuffix && !showCount) {
        return null;
      }
      return /*#__PURE__*/React__default.createElement("span", {
        className: "tree-node-suffix"
      }, showCount && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(IconFont, {
        type: "icon-S_Place_Place"
      }), " ", /*#__PURE__*/React__default.createElement("i", {
        className: "count"
      }, thousand(node.communityCount))), renderSuffix && renderSuffix(node));
    };
    const {
      expandedKeys: _expandedKeys,
      keyword
    } = this.props;
    this.state = {
      keyword: keyword || null,
      expandedKeys: _expandedKeys || [],
      isKeywordChange: false
    };
    // this.oldkeys = null;
  }
  static getDerivedStateFromProps(props, state) {
    let options = {};
    if (!state.expandedKeys && props.expandedKeys) {
      options.expandedKeys = props.expandedKeys;
    }
    if (props.expandedKeys && !isEqual(state.expandedKeys, props.expandedKeys)) {
      options.expandedKeys = props.expandedKeys;
    }
    if (props.keyword !== undefined && props.keyword !== state.keyword) {
      options.keyword = props.keyword;
      options.isKeywordChange = true;
    }
    if (Object.keys(options).length > 0) {
      return options;
    }
    return null;
  }
  componentDidUpdate() {
    if (this.state.isKeywordChange) {
      this.setState({
        isKeywordChange: false
      });
      this.onChange(this.state.keyword);
    }
  }
  render() {
    const {
      place,
      className,
      title,
      inputPlaceholder,
      disableCheckbox,
      treeNodeClass,
      activeKey,
      treeData,
      hasInput,
      ...props
    } = this.props;
    const {
      expandedKeys
    } = this.state;
    const treeDatas = treeData || treeHelper.computPlaceTree(place.placeList);
    return /*#__PURE__*/React__default.createElement("div", {
      className: `lm-c-place-tree ${className}`
    }, title && /*#__PURE__*/React__default.createElement(TreeTitle, {
      title: title,
      inputPlaceholder: inputPlaceholder,
      onChange: this.onKeywordChange,
      hasInput: hasInput
    }), this.props.renderHeader && this.props.renderHeader(), /*#__PURE__*/React__default.createElement(TreeView, _extends({}, props, {
      treeData: treeDatas,
      onExpand: this.onExpand,
      selectedKeys: activeKey ? Array.isArray(activeKey) ? activeKey : [activeKey] : undefined,
      expandedKeys: expandedKeys,
      treeNodeProps: {
        treeNodeIcon: node => /*#__PURE__*/React__default.createElement(IconFont, {
          type: `${node.level >= 5 ? 'icon-S_Place_Place' : title ? 'icon-S_Photo_ListTree' : 'icon-S_Bar_Add'}`
        }),
        key: 'areaCode',
        nameKey: 'areaName',
        keyword: this.state.keyword,
        suffix: this.renderSuffix,
        treeNodeClass: treeNodeClass,
        disableCheckbox: disableCheckbox,
        RenderTreeTitle: this.props.RenderTreeTitle
      }
    })));
  }
}) || _class) || _class);
PlaceTree.defaultProps = {
  title: '行政区划',
  className: '',
  disableCheckbox: false
};

export { PlaceTree as default };
//# sourceMappingURL=index.js.map
