"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.VideoComponent = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var _boxSlotComponent = _interopRequireDefault(require("../../_components/boxSlotComponent"));
var _locale = require("../../../../src/locale");
var _schemaService = require("../../../_utils/schemaService");
var _renderByFormKey = _interopRequireDefault(require("../../_components/renderByFormKey"));
var _formKeyType = require("../../../_utils/formKeyType");
var _type = require("../../cGroupManage-setter/type");
var _config = require("../config");
var _utils = require("../config/utils");
var _utils2 = require("../../../_utils/utils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var VideoComponent = exports.VideoComponent = function VideoComponent(props) {
  var _useState = (0, _react.useState)(props.value),
    videoData = _useState[0],
    setVideoData = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    videoDataSource = _useState2[0],
    setVideoDataSource = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    videoGroupSource = _useState3[0],
    setVideoGroupSource = _useState3[1];
  var _useState4 = (0, _react.useState)([]),
    relationOption = _useState4[0],
    setRelationOption = _useState4[1];
  (0, _react.useEffect)(function () {
    var schemaService = new _schemaService.SchemaService();
    var gList = schemaService.getVideoListData();
    setVideoDataSource(gList);
    if (videoData.videoComponentKey) {
      getVideoGroupList(videoData.videoComponentKey, gList);
    }
    setRelationOption((0, _formKeyType.getRelationList)());
  }, []);
  (0, _react.useEffect)(function () {
    props.onChange(videoData);
  }, [videoData]);
  var getVideoGroupList = function getVideoGroupList(v, list) {
    var videoList = list ? list : videoDataSource;
    var data = videoList.find(function (item) {
      return item.value === v;
    });
    if (data) {
      setVideoGroupSource(data.groupList);
    }
  };
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.VideoComponent")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: videoData.videoComponentKey,
    dataSource: videoDataSource,
    onChange: function onChange(v) {
      var newVideoData = {
        videoComponentKey: v,
        videoGroupId: '',
        videoDataSourceType: _type.GroupDataSourceType["default"]
      };
      setVideoData(newVideoData);
      getVideoGroupList(v);
    }
  })), videoData.videoComponentKey && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupIndexSelectType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: videoData.videoDataSourceType,
    dataSource: (0, _config.getGroupTypeOptionList)(),
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, videoData, {
        videoDataSourceType: v
      });
      // 抹除状态和后续状态
      setVideoData(newGroupData);
    }
  })), videoData.videoComponentKey && videoData.videoDataSourceType !== _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.VideoComponentId")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: videoData.videoGroupId,
    dataSource: videoGroupSource,
    onChange: function onChange(v) {
      var newVideoData = (0, _extends2["default"])({}, videoData, {
        videoGroupId: v
      });
      setVideoData(newVideoData);
    }
  })), videoData.videoComponentKey && videoData.videoDataSourceType === _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.VideoComponentId")
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: relationOption,
    value: videoData.videoGroupIdCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, ''),
    onChange: function onChange(v) {
      (0, _utils2.handleRenderByFormKeyData)(v, function (newItem) {
        var oldData = videoData.videoGroupIdCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '');
        var newData = (0, _extends2["default"])({}, oldData, newItem);
        setVideoData((0, _extends2["default"])({}, videoData, {
          videoGroupIdCustom: newData
        }));
      });
    }
  })));
};