/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var debounce = require('lodash/debounce');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var BaiduMapPicker = /** @class */ (function (_super) {
    tslib.__extends(BaiduMapPicker, _super);
    function BaiduMapPicker() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            inputValue: '',
            locs: [],
            locIndex: -1,
            sugs: []
        };
        _this.id = amisCore.uuid();
        _this.mapRef = React__default["default"].createRef();
        _this.resultListRef = React__default["default"].createRef();
        _this.search = debounce__default["default"](function () {
            var _a;
            if (_this.state.inputValue) {
                (_a = _this.placeSearch) === null || _a === void 0 ? void 0 : _a.search(_this.state.inputValue);
            }
            else {
                _this.setState({
                    sugs: []
                });
            }
        }, 250, {
            trailing: true,
            leading: false
        });
        return _this;
    }
    BaiduMapPicker.prototype.componentDidMount = function () {
        if (window.AMap) {
            this.initMap();
        }
        else {
            amisCore.loadStyle('https://cache.amap.com/lbs/static/main1119.css');
            amisCore.loadScript("//webapi.amap.com/maps?v=2.0&key=".concat(this.props.ak)).then(this.initMap);
        }
    };
    BaiduMapPicker.prototype.initMap = function () {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var map;
            var _this = this;
            return tslib.__generator(this, function (_a) {
                map = new AMap.Map(this.mapRef.current, {
                    resizeEnable: true,
                    zoom: 13,
                });
                this.map = map;
                map.plugin(["AMap.Geocoder"], function () {
                    //加载地理编码插件
                    _this.geocoder = new AMap.Geocoder({
                        radius: 1000,
                        extensions: "all", //返回地址描述以及附近兴趣点和道路信息，默认“base”
                    });
                });
                map.plugin('AMap.Geolocation', function () {
                    _this.geolocation = new AMap.Geolocation({
                        enableHighAccuracy: true,
                        timeout: 10000,
                        maximumAge: 0,
                        convert: true //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
                    });
                    map.addControl(_this.geolocation);
                });
                AMap.plugin(["AMap.PlaceSearch"], function () {
                    //构造地点查询类
                    _this.placeSearch = new AMap.PlaceSearch({
                        pageSize: 5,
                        pageIndex: 1,
                        map: map,
                        panel: _this.resultListRef.current,
                        autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
                    });
                    _this.placeSearch.on("selectChanged", function (e) {
                        var poi = e.selected.data.location;
                        _this.syncLocation({
                            lng: poi.lng,
                            lat: poi.lat,
                            address: e.selected.data.address,
                            city: e.selected.data.cityname,
                        });
                    });
                });
                if (this.props.value) {
                    map.setZoomAndCenter(13, [this.props.value.lng, this.props.value.lat]);
                    this.mark = new AMap.Marker({
                        position: [this.props.value.lng, this.props.value.lat],
                    });
                    this.mark.setMap(map);
                }
                else {
                    this.geolocation.getCurrentPosition(function (status, result) { });
                }
                map.on("click", function (e) {
                    map.setCenter(e.lnglat);
                    var marker = new AMap.Marker({
                        position: [e.lnglat.lng, e.lnglat.lat],
                    });
                    marker.setMap(map);
                    if (_this.mark) {
                        _this.mark.setMap(null);
                    }
                    _this.mark = marker;
                    _this.syncLocation(e.lnglat);
                });
                return [2 /*return*/];
            });
        });
    };
    BaiduMapPicker.prototype.syncLocation = function (lnglat) {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _this = this;
            return tslib.__generator(this, function (_a) {
                new Promise(function (resolve, reject) {
                    var _a, _b;
                    if (lnglat.address && lnglat.city) {
                        var data = {
                            lng: lnglat.lng,
                            lat: lnglat.lat,
                            address: lnglat.address,
                            city: lnglat.city,
                            vendor: "gaode",
                        };
                        if ((_a = _this.props) === null || _a === void 0 ? void 0 : _a.onChange)
                            (_b = _this.props) === null || _b === void 0 ? void 0 : _b.onChange(data);
                    }
                    _this.geocoder.getAddress(lnglat, function (status, result) {
                        var _a, _b;
                        if (status === "complete" && result.info === "OK") {
                            resolve({ status: status, result: result });
                            var data = {
                                lng: lnglat.lng,
                                lat: lnglat.lat,
                                address: result.regeocode.formattedAddress,
                                city: result.regeocode.addressComponent.city ||
                                    result.regeocode.addressComponent.province ||
                                    result.regeocode.addressComponent.district,
                                vendor: "gaode",
                            };
                            if ((_a = _this.props) === null || _a === void 0 ? void 0 : _a.onChange)
                                (_b = _this.props) === null || _b === void 0 ? void 0 : _b.onChange(data);
                        }
                        else {
                            reject({ status: status, result: result });
                        }
                    });
                });
                return [2 /*return*/];
            });
        });
    };
    BaiduMapPicker.prototype.handleChange = function (e) {
        this.setState({
            inputValue: e.currentTarget.value
        }, this.search);
    };
    BaiduMapPicker.prototype.render = function () {
        var cx = this.props.classnames;
        var _a = this.state; _a.locIndex; _a.locs; var inputValue = _a.inputValue; _a.sugs;
        // const hasSug = Array.isArray(sugs) && sugs.length;
        return (_J$X_("div", { className: cx('MapPicker') },
            _J$X_("div", { className: cx('MapPicker-search TextControl-control') },
                _J$X_("div", { className: cx('TextControl-input') },
                    _J$X_("input", { onChange: this.handleChange, value: inputValue, placeholder: "\u641C\u7D22\u5730\u70B9" }))),
            _J$X_("div", { ref: this.mapRef, className: cx('MapPicker-map', {
                    invisible: false
                }) }),
            _J$X_("div", { ref: this.resultListRef, className: cx('MapPicker-result', {
                    invisible: false
                }) })));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", Promise)
    ], BaiduMapPicker.prototype, "initMap", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", Promise)
    ], BaiduMapPicker.prototype, "syncLocation", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], BaiduMapPicker.prototype, "handleChange", null);
    return BaiduMapPicker;
}(React__default["default"].Component));
var GaodeMapPicker = amisCore.themeable(BaiduMapPicker);

exports.BaiduMapPicker = BaiduMapPicker;
exports["default"] = GaodeMapPicker;
