function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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 { 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;