function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 { IconFont } from '@zgfe/business-lib';
import { Button, Row, Spin } from 'antd';
import React, { createContext, useEffect, useState } from 'react';
import useDataList from '../../../hooks/useDataList';
import { classPrefix } from '../types';
import IdSourceItem from './idSourceItem';
export var DatasetListContext = /*#__PURE__*/createContext({
  datasetList: []
});
var IdSource = function IdSource(_ref) {
  var isSingle = _ref.isSingle,
    value = _ref.value,
    fieldType = _ref.fieldType,
    onChange = _ref.onChange;
  var prefix = "".concat(classPrefix, "-create");
  var _useDataList = useDataList(),
    datasetList = _useDataList.datasetList,
    loading = _useDataList.loading;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    list = _useState2[0],
    setList = _useState2[1];
  useEffect(function () {
    if (!value || !value.length) {
      onAdd();
      return;
    }
    setList(value.map(function (val) {
      return {
        id: Math.random(),
        value: val
      };
    }));
  }, []);
  useEffect(function () {
    if (!onChange) return;
    onChange(list.map(function (item) {
      return item.value;
    }));
  }, [list]);
  useEffect(function () {
    if (!isSingle) return;
    setList(function (list) {
      return list.length ? [list[0]] : [];
    });
  }, [isSingle]);
  function onAdd() {
    setList(function (v) {
      return [].concat(_toConsumableArray(v), [{
        id: Math.random()
      }]);
    });
  }
  function onDel(item) {
    setList(function (v) {
      return v.filter(function (_item) {
        return _item.id !== item.id;
      });
    });
  }
  if (loading) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefix, "-source-wrapper")
    }, /*#__PURE__*/React.createElement(Spin, {
      className: "".concat(prefix, "-loading")
    }));
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-source-wrapper")
  }, /*#__PURE__*/React.createElement(DatasetListContext.Provider, {
    value: {
      datasetList: datasetList
    }
  }, list.map(function (item) {
    return /*#__PURE__*/React.createElement(IdSourceItem, {
      key: item.id,
      isSingle: isSingle,
      defaultValue: item.value,
      fieldType: fieldType,
      length: list.length,
      onChange: function onChange(val) {
        setList(function (v) {
          return v.map(function (_ref2) {
            var id = _ref2.id,
              value = _ref2.value;
            return {
              id: id,
              value: id === item.id ? val : value
            };
          });
        });
      },
      onDel: onDel.bind(undefined, item)
    });
  })), list.length < 3 && !isSingle && /*#__PURE__*/React.createElement(Row, {
    className: "".concat(prefix, "-source-wrapper-add")
  }, /*#__PURE__*/React.createElement(Button, {
    type: "link",
    onClick: onAdd
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "zengjia",
    style: {
      marginRight: '8px'
    }
  }), "\u65B0\u589E\u6620\u5C04\u8868"))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-source-tip")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishiicon"
  }), "\u591AID\u60C5\u51B5\u4E0B\uFF0CID\u6765\u6E90\u6700\u591A\u652F\u6301\u4E09\u4E2A\u6620\u5C04\u8868\u3002"));
};
export default IdSource;