"use strict";

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

require("maycur-antd/lib/select/style/css");

var _select = _interopRequireDefault(require("maycur-antd/lib/select"));

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

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _mkTranslate = _interopRequireDefault(require("../utils/mkTranslate"));

var _lodash = _interopRequireDefault(require("lodash"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || 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); }

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; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var Option = _select["default"].Option;

var BankBranchInput =
/*#__PURE__*/
function (_Component) {
  _inherits(BankBranchInput, _Component);

  function BankBranchInput(props) {
    var _this;

    _classCallCheck(this, BankBranchInput);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(BankBranchInput).call(this, props));

    _this.localeProcess = function () {
      _mkTranslate["default"].getNameSpace('PaymentAccountModal').then(function (t) {
        _this.setState({
          t: t
        });
      });
    };

    _this.onChange = function (value) {
      var onChange = _this.props.onChange;

      if (typeof onChange === 'function') {
        var showBranchNo = false;
        var item;

        if (value) {
          item = _this.state.branches.find(function (item) {
            return item.code === value;
          });
          if (item) showBranchNo = !!item.showBranchNo;
        }

        onChange(value, {
          props: {
            showBranchNo: showBranchNo,
            data: item
          }
        });
      }
    };

    _this.searchBranches = function (value) {
      var branches = value ? _this.branches.filter(function (item) {
        return item.name.indexOf(value) > -1;
      }) : _toConsumableArray(_this.branches);
      if (value && branches.every(function (_ref) {
        var name = _ref.name;
        return name !== value;
      })) branches.unshift({
        showBranchNo: true,
        code: value,
        name: "".concat(value)
      });

      _this.setState({
        branches: branches
      });
    };

    _this.state = {
      branches: [],
      // rc-form组件契约：接受value属性成为受控组件
      value: props.value || undefined,
      t: _mkTranslate["default"].noop()
    };
    _this.branches = [];
    _this.searchBranches = (0, _debounce["default"])(_this.searchBranches, 800);
    _this.fetchBankBranches = _this.fetchBankBranches.bind(_assertThisInitialized(_this));

    _this.localeProcess();

    return _this;
  }

  _createClass(BankBranchInput, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchBankBranches();
    } // rc-form组件契约：接受value属性成为受控组件

  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var _this$props = this.props,
          cityCode = _this$props.cityCode,
          bankCode = _this$props.bankCode,
          value = _this$props.value;

      if (nextProps.value !== value) {
        var _value = nextProps.value;
        this.setState({
          value: _value
        });
      }

      if (cityCode !== nextProps.cityCode || bankCode !== nextProps.bankCode) {
        this.fetchBankBranches(_objectSpread({}, nextProps, {
          value: undefined
        }));
        this.setState({
          value: undefined
        }); // if (this.state.value) {

        this.onChange(); // }
      }
    }
  }, {
    key: "fetchBankBranches",
    value: function fetchBankBranches(params) {
      var _this2 = this;

      var _ref2 = params || this.props,
          cityCode = _ref2.cityCode,
          bankCode = _ref2.bankCode,
          value = _ref2.value,
          bankBranchName = _ref2.bankBranchName;

      var _this$props2 = this.props,
          fetchBankBranches = _this$props2.fetchBankBranches,
          showBranchNoChange = _this$props2.showBranchNoChange;

      if (!cityCode || !bankCode || typeof fetchBankBranches !== 'function') {
        this.branches = [];
        this.setState({
          branches: []
        });
        showBranchNoChange(!!value);
      } else {
        fetchBankBranches({
          cityCode: cityCode,
          bankCode: bankCode
        }).then(function (branches) {
          _this2.branches = _toConsumableArray(branches); // 判断外部导入的支行是否在 branches 中，不存在要加进去，否则无法在 Select 中显示

          var isExist = _lodash["default"].find(branches, {
            code: value
          });

          if (value && !isExist) {
            _this2.branches.unshift({
              code: value,
              name: bankBranchName
            });
          }

          _this2.setState({
            branches: _this2.branches
          }); // 判断所选分支行是否为自定义输入


          showBranchNoChange(value && !isExist);
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          branches = _this$state.branches,
          value = _this$state.value,
          t = _this$state.t;
      var disabled = this.props.disabled;
      return _react["default"].createElement(_select["default"], _extends({}, this.props, {
        filterOption: false,
        defaultActiveFirstOption: false,
        showSearch: true,
        onSearch: this.searchBranches,
        onChange: this.onChange,
        value: value,
        disabled: disabled
      }), branches.map(function (city) {
        return _react["default"].createElement(Option, {
          key: city.code,
          value: city.code,
          title: city.name,
          data: city
        }, city.name);
      }), _react["default"].createElement(Option, {
        key: "label",
        disabled: true,
        style: {
          textAlign: 'center',
          paddingBottom: 20
        }
      }, _react["default"].createElement("p", null, t('bankBranchInput.label')), _react["default"].createElement("p", {
        style: {
          color: '#3296FA'
        }
      }, t('bankBranchInput.example'))));
    }
  }]);

  return BankBranchInput;
}(_react.Component);

var _default = BankBranchInput;
exports["default"] = _default;