function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { VtxSearchMap, VtxSearchCheckMap } from '@vtx/map';
import getVtxToken from '@vtx/utils/lib/getVtxToken';
import http from '@vtx/utils/lib/request';
import { Input, Tooltip, Space, Button } from 'antd';
import PropTypes from 'prop-types';
import { forwardRef, useEffect, useState } from 'react';

// 默认经纬度
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var DEFAULT_MAP_CENTER = [116.404, 39.915];
// 通过经纬度获取地址信息
var GeoconvertPath = '/vortexapi/rest/lbs/geoconvert/v2';
// 获取租户信息
var GetTenantByIdPath = '/cloud/management/rest/np/tenant/getTenantById';
// 租户ID
var TENANTID = getVtxToken('tenantId');
var VtxSearchMapInput = /*#__PURE__*/forwardRef(function (props, ref) {
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var _useState3 = useState({
      mapType: '',
      mapCenter: DEFAULT_MAP_CENTER,
      coordinate: '',
      mapServer: ''
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    mapInfo = _useState4[0],
    setMapInfo = _useState4[1];
  var _useState5 = useState(),
    _useState6 = _slicedToArray(_useState5, 2),
    address = _useState6[0],
    setAddress = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    lglt = _useState8[0],
    setLglt = _useState8[1];
  var value = props.value,
    _props$location = props.location,
    location = _props$location === void 0 ? [] : _props$location,
    onChange = props.onChange,
    _props$geoconvertPath = props.geoconvertPath,
    geoconvertPath = _props$geoconvertPath === void 0 ? GeoconvertPath : _props$geoconvertPath,
    _props$getTenantByIdP = props.getTenantByIdPath,
    getTenantByIdPath = _props$getTenantByIdP === void 0 ? GetTenantByIdPath : _props$getTenantByIdP,
    _props$tenantId = props.tenantId,
    tenantId = _props$tenantId === void 0 ? TENANTID : _props$tenantId,
    disabled = props.disabled,
    _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? '请获取地址信息' : _props$placeholder,
    style = props.style,
    mapProps = props.mapProps,
    _props$isPointCheck = props.isPointCheck,
    isPointCheck = _props$isPointCheck === void 0 ? false : _props$isPointCheck,
    _props$geoconvertPara = props.geoconvertParams,
    geoconvertParams = _props$geoconvertPara === void 0 ? {} : _props$geoconvertPara,
    _props$customEnumText = props.customEnumText,
    customEnumText = _props$customEnumText === void 0 ? {} : _props$customEnumText;
  var TextEnum = _objectSpread({
    btnText: '去地图'
  }, customEnumText);
  useEffect(function () {
    http.get(getTenantByIdPath, {
      data: {
        parameters: JSON.stringify({
          id: tenantId
        })
      }
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        var _ref = res.data || {},
          mapDefJson = _ref.mapDefJson,
          longitudeDone = _ref.longitudeDone,
          latitudeDone = _ref.latitudeDone;
        if (Object.prototype.toString.call(mapDefJson) === '[object String]') {
          var mapJson = JSON.parse(mapDefJson);
          mapJson.map(function (item) {
            if (item.defaultMap) {
              setMapInfo({
                mapType: item.mapType,
                coordinate: item.coordinate,
                mapCenter: latitudeDone && longitudeDone ? [longitudeDone, latitudeDone] : [],
                olProps: {
                  olCoverage: item === null || item === void 0 ? void 0 : item.olCoverage,
                  projection: item === null || item === void 0 ? void 0 : item.projection
                }
              });
            }
          });
        }
      }
    });
  }, []);
  var onOk = function onOk(lglt) {
    if (lglt) {
      http.get(geoconvertPath, {
        data: _objectSpread({
          coordtype: mapInfo.coordinate,
          location: lglt.join(',')
        }, geoconvertParams)
      }).then(function (res) {
        var _res$data;
        var address = res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.address;
        onChange === null || onChange === void 0 || onChange(address, {
          lglt: lglt,
          longitude: lglt[0],
          latitude: lglt[1]
        });
      });
    } else {
      address && setAddress();
    }
    setLglt(lglt || []);
    setVisible(false);
  };
  var handleChange = function handleChange(e) {
    var _lglt = lglt.length > 0 ? lglt : location;
    onChange === null || onChange === void 0 || onChange(e.target.value, {
      lglt: _lglt,
      longitude: _lglt[0],
      latitude: _lglt[1]
    });
  };
  console.log('mapInfo.mapType', mapInfo.mapType);
  console.log('isPointCheck', isPointCheck);
  console.log('visible', visible);
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(Tooltip, {
      title: value,
      children: /*#__PURE__*/_jsxs(Space.Compact, {
        children: [/*#__PURE__*/_jsx(Input, {
          ref: ref,
          placeholder: placeholder,
          value: value,
          disabled: disabled || location.length == 0,
          style: style,
          onChange: handleChange
        }), !disabled && /*#__PURE__*/_jsx(Button, {
          onClick: function onClick() {
            return setVisible(true);
          },
          children: /*#__PURE__*/_jsx("a", {
            children: TextEnum.btnText
          })
        })]
      })
    }), mapInfo.mapType && (isPointCheck ? /*#__PURE__*/_jsx(VtxSearchCheckMap, _objectSpread(_objectSpread({}, mapInfo), {}, {
      mapCenter: location[0] ? location : mapInfo.mapCenter,
      modal1Visible: visible,
      graphicType: "point",
      callback: onOk,
      closeModal: function closeModal() {
        return setVisible(false);
      },
      class: "vtx-search-map-model"
    }, mapProps)) : /*#__PURE__*/_jsx(VtxSearchMap, _objectSpread(_objectSpread({}, mapInfo), {}, {
      mapCenter: location[0] ? location : mapInfo.mapCenter,
      modal1Visible: visible,
      graphicType: "point",
      callback: onOk,
      closeModal: function closeModal() {
        return setVisible(false);
      },
      class: "vtx-search-map-model"
    }, mapProps)))]
  });
});
VtxSearchMapInput.propTypes = {
  value: PropTypes.string,
  location: PropTypes.array,
  placeholder: PropTypes.string,
  onChange: PropTypes.func,
  geoconvertPath: PropTypes.string,
  getTenantByIdPath: PropTypes.string,
  tenantId: PropTypes.string,
  disabled: PropTypes.bool,
  style: PropTypes.object,
  mapProps: PropTypes.object,
  isPointCheck: PropTypes.bool,
  geoconvertParams: PropTypes.object,
  customEnumText: PropTypes.object
};
export { VtxSearchMapInput };
export default VtxSearchMapInput;