function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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(''),
    _useState10 = _slicedToArray(_useState9, 2),
    idAlias = _useState10[0],
    setIdAlias = _useState10[1];
  // id名称（default）
  var _useState11 = useState(''),
    _useState12 = _slicedToArray(_useState11, 2),
    idDefault = _useState12[0],
    setIdDefault = _useState12[1];
  var _useState13 = useState(false),
    _useState14 = _slicedToArray(_useState13, 2),
    showAlias = _useState14[0],
    setShowAlias = _useState14[1];
  var _useState15 = useState(),
    _useState16 = _slicedToArray(_useState15, 2),
    idFieldType = _useState16[0],
    setIdFieldType = _useState16[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;