import _extends from "@babel/runtime/helpers/extends";
import _Select from "@alifd/next/es/select";
import React, { useEffect, useState } from "react";
import BoxSlotComponent from "../../_components/boxSlotComponent";
import { t } from "../../../../src/locale";
import { SchemaService } from "../../../_utils/schemaService";
import RenderByFormKey from "../../_components/renderByFormKey";
import { DataSourceType, getRelationList } from "../../../_utils/formKeyType";
import { GroupDataSourceType } from "../../cGroupManage-setter/type";
import { getGroupTypeOptionList } from "../config";
import { getInitStateKey } from "../config/utils";
import { handleRenderByFormKeyData } from "../../../_utils/utils";
export var VideoComponent = function VideoComponent(props) {
  var _useState = useState(props.value),
    videoData = _useState[0],
    setVideoData = _useState[1];
  var _useState2 = useState([]),
    videoDataSource = _useState2[0],
    setVideoDataSource = _useState2[1];
  var _useState3 = useState([]),
    videoGroupSource = _useState3[0],
    setVideoGroupSource = _useState3[1];
  var _useState4 = useState([]),
    relationOption = _useState4[0],
    setRelationOption = _useState4[1];
  useEffect(function () {
    var schemaService = new SchemaService();
    var gList = schemaService.getVideoListData();
    setVideoDataSource(gList);
    if (videoData.videoComponentKey) {
      getVideoGroupList(videoData.videoComponentKey, gList);
    }
    setRelationOption(getRelationList());
  }, []);
  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.createElement("div", null, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.VideoComponent")
  }, /*#__PURE__*/React.createElement(_Select, {
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    showSearch: true,
    value: videoData.videoComponentKey,
    dataSource: videoDataSource,
    onChange: function onChange(v) {
      var newVideoData = {
        videoComponentKey: v,
        videoGroupId: '',
        videoDataSourceType: GroupDataSourceType["default"]
      };
      setVideoData(newVideoData);
      getVideoGroupList(v);
    }
  })), videoData.videoComponentKey && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("page.groupIndexSelectType")
  }, /*#__PURE__*/React.createElement(_Select, {
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    showSearch: true,
    value: videoData.videoDataSourceType,
    dataSource: getGroupTypeOptionList(),
    onChange: function onChange(v) {
      var newGroupData = _extends({}, videoData, {
        videoDataSourceType: v
      });
      // 抹除状态和后续状态
      setVideoData(newGroupData);
    }
  })), videoData.videoComponentKey && videoData.videoDataSourceType !== GroupDataSourceType.custom && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.VideoComponentId")
  }, /*#__PURE__*/React.createElement(_Select, {
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    showSearch: true,
    value: videoData.videoGroupId,
    dataSource: videoGroupSource,
    onChange: function onChange(v) {
      var newVideoData = _extends({}, videoData, {
        videoGroupId: v
      });
      setVideoData(newVideoData);
    }
  })), videoData.videoComponentKey && videoData.videoDataSourceType === GroupDataSourceType.custom && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.VideoComponentId")
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: relationOption,
    value: videoData.videoGroupIdCustom || getInitStateKey(DataSourceType.defaultType, ''),
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var oldData = videoData.videoGroupIdCustom || getInitStateKey(DataSourceType.defaultType, '');
        var newData = _extends({}, oldData, newItem);
        setVideoData(_extends({}, videoData, {
          videoGroupIdCustom: newData
        }));
      });
    }
  })));
};