"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = ImageSetting;

var _fdx = require("@whalecloud/fdx");

var _react = _interopRequireWildcard(require("react"));

var _IconBase = require("../../../../../../assets/IconBase64");

var _anchor = _interopRequireDefault(require("../../../../../../assets/imgs/anchor.svg"));

var _CommonMaterialLibrary = _interopRequireDefault(require("../../../components/CommonMaterialLibrary"));

var _LinkModal = _interopRequireDefault(require("../../../components/LinkModal"));

var _AnchorModal = _interopRequireDefault(require("../AnchorModal"));

var _imgArea = _interopRequireDefault(require("../img-area"));

require("./index.less");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

function ImageSetting(props) {
  var coordinates = props.coordinates,
      imgUrl = props.imgUrl,
      _onChange = props.onChange,
      anchors = props.anchors;

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

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

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

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

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

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

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

  var setArea = function setArea() {
    !picUrl ? _fdx.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["default"].createElement("div", {
      className: "imgItem"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "imgUpload"
    }, hasImg ? /*#__PURE__*/_react["default"].createElement("div", {
      className: "upload"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "imgModal"
    }, /*#__PURE__*/_react["default"].createElement(_fdx.Icon, {
      onClick: function onClick() {
        window.open(picUrl);
      },
      type: "eye",
      style: {
        color: '#fff',
        fontSize: 16,
        marginRight: 16,
        cursor: 'pointer'
      }
    }), /*#__PURE__*/_react["default"].createElement(_fdx.Icon, {
      onClick: function onClick() {
        setImgUrl('');

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

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

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