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 { BizSelect, BizCycleTime, IconFont } from '@zgfe/business-lib';
import { Form, Radio, Spin, Tooltip, AutoComplete } from 'antd';
import React, { useContext, useEffect, useRef, useState } from 'react';
import TaskContext from '../../modules/createJob/context';
import ZModulesCDP from '../../types';
import { timeUnitList } from '../../modules/dataFusion/components/fusionModal/utils';
import './styles/updateForm.less';
import _ from 'lodash';
var classPrefix = 'cdp-dscreator-update';
export var SyncType = function SyncType(props) {
  var columnList = props.columnList,
    dbType = props.dbType;
  var _useContext = useContext(TaskContext),
    mode = _useContext.mode;
  var _useState = useState(ZModulesCDP.DataSet.SyncType.global),
    _useState2 = _slicedToArray(_useState, 2),
    syncType = _useState2[0],
    setSyncType = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    column = _useState4[0],
    setColumn = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    columns = _useState6[0],
    setColumns = _useState6[1];
  var _useState7 = useState('yyyyMMddHHmmss'),
    _useState8 = _slicedToArray(_useState7, 2),
    dateFormat = _useState8[0],
    setDateFormat = _useState8[1];
  var _useState9 = useState(false),
    _useState0 = _slicedToArray(_useState9, 2),
    onChangeFlag = _useState0[0],
    setonChangeFlag = _useState0[1];
  /**
   * 初始化表单
   */
  useEffect(function () {
    var _props$value, _props$value3;
    if ((_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.syncType) {
      var _props$value2;
      // if (mode === 'create') return;
      setSyncType((_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.syncType);
    }
    if ((_props$value3 = props.value) === null || _props$value3 === void 0 ? void 0 : _props$value3.updateBy) {
      var _props$value4, _props$value5, _props$value5$updateB;
      setColumn((_props$value4 = props.value) === null || _props$value4 === void 0 ? void 0 : _props$value4.updateBy);
      setDateFormat((_props$value5 = props.value) === null || _props$value5 === void 0 ? void 0 : (_props$value5$updateB = _props$value5.updateBy) === null || _props$value5$updateB === void 0 ? void 0 : _props$value5$updateB.dateFormat);
    }
  }, []);
  useEffect(function () {
    if (dbType && /mongodb|hive|api/i.test(dbType)) {
      setSyncType(ZModulesCDP.DataSet.SyncType.global);
    }
  }, [dbType]);
  useEffect(function () {
    if (!onChangeFlag) {
      return;
    }
    var _temp = {};
    if ((column === null || column === void 0 ? void 0 : column.webType) === 'date') {
      _temp = {
        dateFormat: dateFormat
      };
    }
    if (props.onChange) props.onChange({
      syncType: syncType,
      updateBy: _objectSpread(_objectSpread({}, column), _temp)
    });
  }, [column, syncType, dateFormat, onChangeFlag]);
  useEffect(function () {
    setColumns(columnList.filter(function (item) {
      return ['number', 'date'].includes(item.webType);
    }));
  }, [columnList]);
  useEffect(function () {
    if (!column) return;
    var match = false;
    columns.forEach(function (item) {
      if (item.name === column.name) match = true;
    });
    if (!match && dbType !== 'excel') setColumn(undefined);
  }, [columns]);
  var onChangeFormat = function onChangeFormat(data) {
    var inputValue = _.trim(data);
    setonChangeFlag(true);
    setDateFormat(inputValue);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-sync")
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    value: syncType,
    onChange: function onChange(e) {
      setonChangeFlag(true);
      setSyncType(e.target.value);
    }
  }, /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.SyncType.incre,
    disabled: /mongodb|hive|api/i.test(dbType || '')
  }, "\u589E\u91CF\u66F4\u65B0", /*#__PURE__*/React.createElement(Tooltip, {
    title: "\u589E\u91CF\u66F4\u65B0\u8868\u793A\u4EFB\u52A1\u53EA\u66F4\u65B0\u53D1\u751F\u53D8\u5316\u7684\u6570\u636E\uFF0C\u53EA\u80FD\u9009\u62E9\u65E5\u671F\u548C\u6570\u503C\u7C7B\u578B\u7684\u5B57\u6BB5\u3002"
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishi",
    style: {
      marginLeft: '4px',
      color: '#9aa1a9',
      verticalAlign: '-1px'
    }
  }))), /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.SyncType.global
  }, "\u5168\u91CF\u66F4\u65B0")), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-incre-box")
  }, syncType === ZModulesCDP.DataSet.SyncType.incre && (/*#__PURE__*/React.createElement(BizSelect, {
    options: dbType === 'excel' ? [{
      name: 'createTime',
      label: '按文件创建日期'
    }, {
      name: 'updateTime',
      label: '按文件修改日期'
    }] : columns,
    keyField: "name",
    labelField: dbType === 'excel' ? 'label' : 'name',
    placeholder: "\u9009\u62E9\u5B57\u6BB5",
    enableSearch: true,
    value: column,
    disable: syncType !== ZModulesCDP.DataSet.SyncType.incre,
    customLabel: function customLabel(label, data) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, label, data.isPrimaryKey ? '(主键)' : '');
    },
    onChange: function onChange(v) {
      setonChangeFlag(true);
      setColumn(v);
    }
  })), syncType === ZModulesCDP.DataSet.SyncType.incre && (column === null || column === void 0 ? void 0 : column.webType) === 'date' && (/*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-date")
  }, /*#__PURE__*/React.createElement(AutoComplete, {
    allowClear: true,
    maxLength: 20,
    popupClassName: "".concat(classPrefix, "-popup"),
    value: dateFormat,
    style: {
      width: 230
    },
    options: [{
      value: 'yyyyMMddHHmmss',
      label: 'yyyyMMddHHmmss'
    }, {
      value: 'yyyy-MM-dd HH:mm:ss',
      label: 'yyyy-MM-dd HH:mm:ss'
    }, {
      value: 'yyyy/MM/dd HH:mm:ss',
      label: 'yyyy/MM/dd HH:mm:ss'
    }],
    placeholder: "\u8BF7\u8F93\u5165\u65E5\u671F\u683C\u5F0F",
    onChange: onChangeFormat
  }), /*#__PURE__*/React.createElement(Tooltip, {
    title: "\u201Cy\u201D\u4EE3\u8868\u201C\u5E74\u201D\uFF0C\u201CM\u201D\u4EE3\u8868\u201C\u6708\u201D\uFF0C\u201Cd\u201D\u4EE3\u8868\u201C\u5929\u201D\uFF0C\u4F8B\u5982\u683C\u5F0F\u201Cyyyy-MM-dd\u201D\u4EE3\u8868\u201C2023-01-01\u201D"
  }, /*#__PURE__*/React.createElement(IconFont, {
    className: "".concat(classPrefix, "-icon"),
    type: "tishi"
  }))))));
};
export var UpdateSetting = function UpdateSetting(props) {
  var _useState1 = useState({}),
    _useState10 = _slicedToArray(_useState1, 2),
    timeData = _useState10[0],
    setTimeData = _useState10[1];
  useEffect(function () {
    if (props.value) {
      setTimeData(props.value);
    } else {
      setTimeData({
        type: ZModulesCDP.DataSet.UpdateType.timer
      });
    }
  }, []);
  useEffect(function () {
    if (props.onChange) props.onChange(timeData);
  }, [timeData]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-update")
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    value: timeData.type,
    onChange: function onChange(e) {
      setTimeData(_objectSpread(_objectSpread({}, timeData), {}, {
        type: e.target.value
      }));
    }
  }, /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.UpdateType.mumal
  }, "\u624B\u52A8"), /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.UpdateType.timer
  }, "\u5B9A\u65F6")), timeData.type === ZModulesCDP.DataSet.UpdateType.timer ? (/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(BizCycleTime, {
    value: timeData.rule,
    size: "middle",
    multiple: true,
    showHour: true,
    format: "HH",
    minTime: "Now",
    options: timeUnitList,
    onChange: function onChange(val) {
      setTimeData(_objectSpread(_objectSpread({}, timeData), {}, {
        rule: {
          unit: val.unit,
          day: val.day,
          time: val.time
        }
      }));
    }
  }))) : null);
};
var UpdateForm = function UpdateForm(props) {
  var _useContext2 = useContext(TaskContext),
    mode = _useContext2.mode;
  var columnList = props.columnList,
    task = props.task;
  var formRef = useRef(null);
  function onChange() {
    var _formRef$current;
    (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.submit();
  }
  function onSubmit(data) {
    if (props.onChange) props.onChange(data);
  }
  var initialValues = {
    sync: {
      syncType: task === null || task === void 0 ? void 0 : task.syncType,
      updateBy: task === null || task === void 0 ? void 0 : task.updateBy
    },
    updateSetting: task === null || task === void 0 ? void 0 : task.updateSetting
  };
  if (mode === 'edit' && props.loading) {
    return /*#__PURE__*/React.createElement(Spin, null);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", null, "\u66F4\u65B0\u7B56\u7565"), /*#__PURE__*/React.createElement(Form, {
    colon: false,
    ref: formRef,
    onFinish: onSubmit,
    initialValues: mode === 'edit' ? initialValues : undefined,
    autoComplete: "off"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u66F4\u65B0\u65B9\u5F0F",
    name: "sync"
  }, /*#__PURE__*/React.createElement(SyncType, {
    columnList: columnList,
    onChange: onChange,
    dbType: props.dbType
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u66F4\u65B0\u8BBE\u7F6E",
    name: "updateSetting"
  }, /*#__PURE__*/React.createElement(UpdateSetting, {
    onChange: onChange
  }))));
};
export default UpdateForm;