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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import { Button, Icon, Input, message, Modal } from '@whalecloud/fdx';
import React, { useEffect, useState } from 'react';
import { unlinkIcon } from '../../../../../../assets/IconBase64';
import anchor from '../../../../../../assets/imgs/anchor.svg';
import CommonMaterialLibrary from '../../../components/CommonMaterialLibrary';
import LinkModal from '../../../components/LinkModal';
import AnchorModal from '../AnchorModal';
import ImgArea from '../img-area';
import './index.less';
export default function ImageSetting(props) {
  var coordinates = props.coordinates,
      imgUrl = props.imgUrl,
      _onChange = props.onChange,
      anchors = props.anchors;

  var _useState = useState(imgUrl || ''),
      _useState2 = _slicedToArray(_useState, 2),
      picUrl = _useState2[0],
      setImgUrl = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      visible = _useState4[0],
      setVisible = _useState4[1];

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      linkVisible = _useState6[0],
      setLinkVisible = _useState6[1];

  var _useState7 = useState(false),
      _useState8 = _slicedToArray(_useState7, 2),
      anchorVisible = _useState8[0],
      setAnchorVisible = _useState8[1];

  var _useState9 = useState(0),
      _useState10 = _slicedToArray(_useState9, 2),
      itemIndex = _useState10[0],
      setItemIndex = _useState10[1];

  var _useState11 = useState(coordinates || []),
      _useState12 = _slicedToArray(_useState11, 2),
      drawCoordinates = _useState12[0],
      setDrawCoordinates = _useState12[1];

  useEffect(function () {
    setImgUrl(imgUrl || '');
  }, [imgUrl]);
  useEffect(function () {
    setDrawCoordinates(coordinates || []);
  }, [coordinates]);

  var setArea = function setArea() {
    !picUrl ? message.warn('Please upload pictures!') : setVisible(true);
  };

  var onOk = function onOk() {
    _onChange(drawCoordinates, picUrl);

    setVisible(false);
  };

  var onCancel = function onCancel() {
    setVisible(false);
  };

  var setCoordinate = function setCoordinate(coordinate, index, coordinates) {
    setDrawCoordinates(coordinates);
  };

  var delItem = function delItem(index) {
    drawCoordinates.splice(index, 1);
    console.log(drawCoordinates, 321);
    setDrawCoordinates(_toConsumableArray(drawCoordinates));
  };

  var add = function add() {
    setDrawCoordinates([].concat(_toConsumableArray(drawCoordinates), [{
      height: 50,
      id: "Hyp_dBIiD".concat(drawCoordinates.length),
      inputVal: '',
      width: 100,
      x: 54 + drawCoordinates.length * 10,
      y: 38 + drawCoordinates.length * 10
    }]));
  };

  var imgItem = function imgItem() {
    var hasImg = !!picUrl;
    return /*#__PURE__*/React.createElement("div", {
      className: "imgItem"
    }, /*#__PURE__*/React.createElement("div", {
      className: "imgUpload"
    }, hasImg ? /*#__PURE__*/React.createElement("div", {
      className: "upload"
    }, /*#__PURE__*/React.createElement("div", {
      className: "imgModal"
    }, /*#__PURE__*/React.createElement(Icon, {
      onClick: function onClick() {
        window.open(picUrl);
      },
      type: "eye",
      style: {
        color: '#fff',
        fontSize: 16,
        marginRight: 16,
        cursor: 'pointer'
      }
    }), /*#__PURE__*/React.createElement(Icon, {
      onClick: function onClick() {
        setImgUrl('');

        _onChange([], '');
      },
      type: "delete",
      style: {
        color: '#fff',
        fontSize: 16,
        cursor: 'pointer'
      }
    })), /*#__PURE__*/React.createElement("img", {
      height: "58",
      src: picUrl,
      alt: ""
    })) : /*#__PURE__*/React.createElement(CommonMaterialLibrary, {
      maxCount: 1,
      value: {},
      onChange: function onChange(value) {
        if (value && value[0]) {
          setImgUrl("".concat(value[0].url || ''));

          _onChange([], "".concat(value[0].url || ''));
        }
      }
    })), /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        setArea();
      },
      style: {
        padding: '0 10px',
        height: '28px',
        margin: '4px 0 16px 0'
      }
    }, "Set Clickable Area"));
  };

  return /*#__PURE__*/React.createElement("div", {
    className: "imgSetting"
  }, /*#__PURE__*/React.createElement("div", {
    className: "imgTitle"
  }, "Image Setting"), /*#__PURE__*/React.createElement("div", {
    className: "imgCot"
  }, imgItem()), /*#__PURE__*/React.createElement(Modal, {
    title: "clickable Area",
    visible: visible,
    onOk: onOk,
    draggable: false,
    onCancel: onCancel,
    width: "auto",
    centered: true
  }, /*#__PURE__*/React.createElement("div", {
    className: "clickableArea"
  }, /*#__PURE__*/React.createElement(ImgArea, {
    src: picUrl // width={window.safeArea || 1200}
    ,
    coordinates: drawCoordinates,
    onChange: setCoordinate,
    onDelete: setCoordinate,
    onSave: setCoordinate
  }), /*#__PURE__*/React.createElement("div", {
    className: "add_click"
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    onClick: add
  }, "Add Clickable Area")), drawCoordinates.map(function (v, i) {
    return /*#__PURE__*/React.createElement("div", {
      className: "add_item"
    }, /*#__PURE__*/React.createElement("div", {
      className: "title"
    }, "Clickable Area ", i + 1), /*#__PURE__*/React.createElement("div", {
      style: {
        display: 'flex',
        marginTop: '8px'
      }
    }, /*#__PURE__*/React.createElement(Input, {
      allowClear: true,
      style: {
        marginRight: '4px'
      },
      value: v.inputVal,
      onChange: function onChange(e) {
        v.inputVal = e.currentTarget.value;
        setDrawCoordinates(_toConsumableArray(drawCoordinates));
      }
    }), /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        setItemIndex(i);
        setAnchorVisible(true);
      },
      style: {
        padding: '0 7px',
        height: '28px',
        marginRight: '4px'
      }
    }, /*#__PURE__*/React.createElement("img", {
      className: "linkBut",
      width: "14",
      src: anchor,
      alt: ""
    })), /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        setItemIndex(i);
        setLinkVisible(true);
      },
      style: {
        padding: '0 7px',
        height: '28px',
        marginRight: '4px'
      }
    }, /*#__PURE__*/React.createElement("img", {
      className: "linkBut",
      width: "14",
      src: unlinkIcon,
      alt: ""
    })), /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        delItem(i);
      },
      style: {
        padding: '0 7px',
        height: '28px'
      }
    }, /*#__PURE__*/React.createElement(Icon, {
      type: "delete"
    }))));
  }))), /*#__PURE__*/React.createElement(LinkModal // renderer={renderer}
  , {
    visible: linkVisible,
    handleModalSave: function handleModalSave(url) {
      drawCoordinates[itemIndex].inputVal = url;
      setLinkVisible(false);
    },
    closeModal: function closeModal() {
      setLinkVisible(false);
    }
  }), /*#__PURE__*/React.createElement(AnchorModal, {
    visible: anchorVisible,
    setVisible: setAnchorVisible,
    anchors: anchors || [],
    value: drawCoordinates[itemIndex] ? drawCoordinates[itemIndex].inputVal : '',
    onSave: function onSave(url) {
      drawCoordinates[itemIndex].inputVal = url;
      setAnchorVisible(false);
    }
  })));
}