import _extends from "@babel/runtime/helpers/extends";
import _Select from "@alifd/next/es/select";
import React, { useEffect, useState } from "react";
import { TelegramType, TouchSelectType } from "../type";
import BoxSlotComponent from "../../_components/boxSlotComponent";
import RenderByFormKey from "../../_components/renderByFormKey";
import { t } from "../../../../src/locale";
import { DataSourceType, getRelationList } from "../../../_utils/formKeyType";
import { handleRenderByFormKeyData } from "../../../_utils/utils";
import { getInitStateKey, handleStringToRequest } from "../config/utils";
var TelegramOptions = [{
  label: t("setter.share"),
  value: TelegramType.share
}, {
  label: t("setter.link"),
  value: TelegramType.link
}, {
  label: t("setter.touch"),
  value: TelegramType.touch
}];
var TouchOptions = [{
  label: t("setter.touchSoft"),
  value: TouchSelectType.soft
}, {
  label: t("setter.touchRigid"),
  value: TouchSelectType.rigid
}, {
  label: t("setter.touchLight"),
  value: TouchSelectType.light
}, {
  label: t("setter.touchMedium"),
  value: TouchSelectType.medium
}, {
  label: t("setter.touchHeavy"),
  value: TouchSelectType.heavy
}];
export var TelegramComponent = function TelegramComponent(props) {
  var _value$data, _value$data2, _value$data2$shareDat, _value$data3, _value$data3$shareDat, _value$data4, _value$data4$shareDat, _value$data5;
  var value = props.value,
    onChange = props.onChange;
  var _useState = useState((value === null || value === void 0 ? void 0 : value.type) || TelegramType.share),
    type = _useState[0],
    setType = _useState[1];
  var _useState2 = useState((value === null || value === void 0 ? void 0 : (_value$data = value.data) === null || _value$data === void 0 ? void 0 : _value$data.linkUrl) || getInitStateKey(DataSourceType.defaultType, '')),
    linkUrl = _useState2[0],
    setLinkUrl = _useState2[1];
  var _useState3 = useState(handleStringToRequest(value === null || value === void 0 ? void 0 : (_value$data2 = value.data) === null || _value$data2 === void 0 ? void 0 : (_value$data2$shareDat = _value$data2.shareData) === null || _value$data2$shareDat === void 0 ? void 0 : _value$data2$shareDat.url)),
    shareUrl = _useState3[0],
    setShareUrl = _useState3[1];
  var _useState4 = useState((value === null || value === void 0 ? void 0 : (_value$data3 = value.data) === null || _value$data3 === void 0 ? void 0 : (_value$data3$shareDat = _value$data3.shareData) === null || _value$data3$shareDat === void 0 ? void 0 : _value$data3$shareDat.startApp) || getInitStateKey(DataSourceType.defaultType, '')),
    shareStartApp = _useState4[0],
    setShareStartApp = _useState4[1];
  var _useState5 = useState(handleStringToRequest(value === null || value === void 0 ? void 0 : (_value$data4 = value.data) === null || _value$data4 === void 0 ? void 0 : (_value$data4$shareDat = _value$data4.shareData) === null || _value$data4$shareDat === void 0 ? void 0 : _value$data4$shareDat.content)),
    shareContent = _useState5[0],
    setShareContent = _useState5[1];
  var _useState6 = useState((value === null || value === void 0 ? void 0 : (_value$data5 = value.data) === null || _value$data5 === void 0 ? void 0 : _value$data5.touchType) || TouchSelectType.soft),
    touchType = _useState6[0],
    setTouchType = _useState6[1];
  var _useState7 = useState([]),
    telegramRelationOption = _useState7[0],
    setTelegramRelationOption = _useState7[1];
  useEffect(function () {
    setTelegramRelationOption(getRelationList());
  }, []);
  useEffect(function () {
    var obj = {
      type: type,
      data: {
        linkUrl: linkUrl,
        shareData: {
          url: shareUrl,
          startApp: shareStartApp,
          content: shareContent
        },
        touchType: touchType
      }
    };
    onChange(obj);
  }, [type, linkUrl, shareUrl, shareStartApp, shareContent, touchType]);
  return /*#__PURE__*/React.createElement("div", {
    className: "awaitComponentBox"
  }, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "" + t('setter.linkType')
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: TelegramOptions,
    value: type,
    onChange: function onChange(v) {
      setType(v);
    }
  })), type === TelegramType.link && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "" + t('setter.linkOriginUrl')
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: telegramRelationOption,
    value: linkUrl,
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newData = _extends({}, linkUrl, newItem);
        setLinkUrl(newData);
      });
    }
  })), type === TelegramType.share && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "" + t('setter.shareLink')
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: telegramRelationOption,
    value: shareUrl,
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newData = _extends({}, shareUrl, newItem);
        setShareUrl(newData);
      });
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "" + t('setter.shareStartAppId')
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: telegramRelationOption,
    value: shareStartApp,
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newData = _extends({}, shareStartApp, newItem);
        setShareStartApp(newData);
      });
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "" + t('setter.shareContent')
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: telegramRelationOption,
    value: shareContent,
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newData = _extends({}, shareContent, newItem);
        setShareContent(newData);
      });
    }
  }))), type === TelegramType.touch && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "" + t('setter.touch')
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: TouchOptions,
    value: touchType,
    onChange: function onChange(v) {
      setTouchType(v);
    }
  })));
};