function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import { BizGlobalDataContext, IconFont } from '@zgfe/business-lib';
import { Input, Radio, Select, Skeleton, Space, Tooltip } from 'antd';
import _ from 'lodash';
import React, { useContext, useEffect, useState } from 'react';
import ZModulesCDP from '../../../types';
import { columnNameReg } from '../../visualJob/create/configPanel/row2Column/columnName';
import { classPrefix } from '../types';
var NameInput = function NameInput(_ref) {
  var value = _ref.value,
    idList = _ref.idList,
    onChange = _ref.onChange;
  var prefix = "".concat(classPrefix, "-create");
  var _useContext = useContext(BizGlobalDataContext),
    userPropList = _useContext.userPropList;
  var _useState = useState('default'),
    _useState2 = _slicedToArray(_useState, 2),
    type = _useState2[0],
    setType = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    ready = _useState4[0],
    setReady = _useState4[1];
  var _useState5 = useState(),
    _useState6 = _slicedToArray(_useState5, 2),
    prop = _useState6[0],
    setProp = _useState6[1];
  // id名称（userProperty）
  var _useState7 = useState(''),
    _useState8 = _slicedToArray(_useState7, 2),
    idName = _useState8[0],
    setIdName = _useState8[1];
  var _useState9 = useState(''),
    _useState0 = _slicedToArray(_useState9, 2),
    idAlias = _useState0[0],
    setIdAlias = _useState0[1];
  // id名称（default）
  var _useState1 = useState(''),
    _useState10 = _slicedToArray(_useState1, 2),
    idDefault = _useState10[0],
    setIdDefault = _useState10[1];
  var _useState11 = useState(false),
    _useState12 = _slicedToArray(_useState11, 2),
    showAlias = _useState12[0],
    setShowAlias = _useState12[1];
  var _useState13 = useState(),
    _useState14 = _slicedToArray(_useState13, 2),
    idFieldType = _useState14[0],
    setIdFieldType = _useState14[1];
  // 初始化默认值
  useEffect(function () {
    setType((value === null || value === void 0 ? void 0 : value.type) || 'default');
    setReady(true);
    if ((value === null || value === void 0 ? void 0 : value.type) && value.type === 'userProperty') {
      setIdName((value === null || value === void 0 ? void 0 : value.idName) || '');
      setIdAlias((value === null || value === void 0 ? void 0 : value.idAlias) || '');
      // 中文时显示
      setShowAlias((value === null || value === void 0 ? void 0 : value.idName) ? /[\u4e00-\u9fa5]/.test(value === null || value === void 0 ? void 0 : value.idName) : false);
    } else {
      setIdDefault((value === null || value === void 0 ? void 0 : value.idName) || (value === null || value === void 0 ? void 0 : value.idAlias) || '');
    }
    setIdFieldType(value === null || value === void 0 ? void 0 : value.idFieldType);
  }, []);
  useEffect(function () {
    if (!onChange) return;
    onChange({
      idName: type === 'userProperty' ? idName : idDefault,
      idAlias: idAlias,
      type: type,
      idFieldType: idFieldType
    });
  }, [idName, idAlias, idDefault, type]);
  function onChangeProp(_v, option) {
    setProp(option);
    setIdName(option.name);
    setIdFieldType([ZModulesCDP.WebType.string, ZModulesCDP.WebType.number, ZModulesCDP.WebType.date][((option === null || option === void 0 ? void 0 : option.type) || 0) - 1]);
    /**
     * 属性name包含中文时，弹出别名输入框；
     */
    setShowAlias(/[\u4e00-\u9fa5]/.test(option.name));
    /**
     * 如果属性原名是中文，但是别名是英文，则自动设置
     */
    if (option.alias && !/[\u4e00-\u9fa5]/.test(option.alias || '')) {
      setIdAlias(option.alias);
    } else {
      setIdAlias('');
    }
  }
  function onChangeIdName(e) {
    e.persist();
    if (!columnNameReg.test(e.target.value)) {
      return;
    }
    setIdDefault(e.target.value);
  }
  function onChangeIdAlias(e) {
    e.persist();
    if (!columnNameReg.test(e.target.value)) {
      return;
    }
    setIdAlias(e.target.value);
  }
  if (!ready) {
    return /*#__PURE__*/React.createElement(Skeleton, null);
  }
  var options = userPropList === null || userPropList === void 0 ? void 0 : userPropList.filter(function (item) {
    return !item.isHidden && item.category !== 'fixed';
  }).map(function (item) {
    return _objectSpread(_objectSpread({}, item), {}, {
      showLabel: /*#__PURE__*/React.createElement(Tooltip, {
        title: item.alias || item.label
      }, item.alias || item.label),
      disabled: idList.filter(function (a) {
        return a.type === 'userProperty';
      }).map(function (a) {
        return a.idName;
      }).includes(item.name)
    });
  });
  if (type === 'fixed') {
    return /*#__PURE__*/React.createElement(Input, {
      value: idDefault,
      disabled: true,
      style: {
        width: 632
      }
    });
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Radio.Group, {
    className: "".concat(prefix, "-name-wrapper"),
    defaultValue: type,
    onChange: function onChange(e) {
      setProp(undefined);
      setIdName('');
      setIdAlias('');
      setIdDefault('');
      setType(e.target.value);
      if (e.target.value === 'default') setShowAlias(false);
    }
  }, /*#__PURE__*/React.createElement(Space, {
    direction: "vertical",
    size: 24
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "default"
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165ID\u540D\u79F0",
    style: {
      width: '592px',
      marginLeft: '4px'
    },
    autoFocus: true,
    value: idDefault,
    disabled: type === 'userProperty',
    onChange: onChangeIdName
  })), /*#__PURE__*/React.createElement(Space, {
    size: 0
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "userProperty",
    style: {
      marginRight: '12px'
    }
  }), /*#__PURE__*/React.createElement(Select, {
    options: options,
    fieldNames: {
      value: 'name',
      label: 'showLabel'
    },
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      className: "".concat(prefix, "-icon")
    }),
    showSearch: true,
    filterOption: function filterOption(search, option) {
      return new RegExp(_.escapeRegExp(search), 'i').test((option === null || option === void 0 ? void 0 : option.alias) || (option === null || option === void 0 ? void 0 : option.label) || '');
    },
    style: {
      width: 272
    },
    value: type === 'userProperty' && idName,
    placeholder: "\u8BF7\u9009\u62E9\u7528\u6237\u5C5E\u6027",
    disabled: type === 'default',
    onChange: onChangeProp
  }), showAlias && (/*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165ID\u540D\u79F0",
    style: {
      width: 308
    },
    autoFocus: true,
    value: idAlias,
    disabled: type === 'default',
    onChange: onChangeIdAlias
  }))))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-name-tip")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishiicon"
  }), "ID - Mapping\u76EE\u524D\u4E0D\u652F\u6301\u4E2D\u6587\u5B57\u6BB5\u5904\u7406\uFF0C\u82E5\u60A8\u9009\u62E9\u7684\u7528\u6237\u5C5E\u6027\u4E3A\u4E2D\u6587\uFF0C\u9700\u8981\u91CD\u65B0\u547D\u540D\u3002"));
};
export default NameInput;