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

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { Overlay, PopOver, autobind, themeable, localeable } from 'amis-core';
import { Icon } from './icons.js';
import Alert2 from './Alert2.js';
import BaiduMapPicker from './BaiduMapPicker.js';
import GaodeMapPicker from './GaodeMapPicker.js';
import PopUp from './PopUp.js';

var LocationPicker = /** @class */ (function (_super) {
    __extends(LocationPicker, _super);
    function LocationPicker() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.domRef = React__default.createRef();
        _this.state = {
            isFocused: false,
            isOpened: false
        };
        return _this;
    }
    LocationPicker.prototype.handleKeyPress = function (e) {
        if (e.key === ' ') {
            this.handleClick();
            e.preventDefault();
        }
    };
    LocationPicker.prototype.handleFocus = function () {
        this.setState({
            isFocused: true
        });
    };
    LocationPicker.prototype.handleBlur = function () {
        this.setState({
            isFocused: true
        });
    };
    LocationPicker.prototype.handleClick = function () {
        this.state.isOpened ? this.close() : this.open();
    };
    LocationPicker.prototype.getTarget = function () {
        return this.domRef.current;
    };
    LocationPicker.prototype.getParent = function () {
        var _a;
        return (_a = this.domRef.current) === null || _a === void 0 ? void 0 : _a.parentElement;
    };
    LocationPicker.prototype.open = function (fn) {
        this.props.disabled ||
            this.setState({
                isOpened: true
            }, fn);
        this.tempValue = this.props.value;
    };
    LocationPicker.prototype.close = function () {
        this.setState({
            isOpened: false
        });
    };
    LocationPicker.prototype.clearValue = function (e) {
        e.preventDefault();
        e.stopPropagation();
        var onChange = this.props.onChange;
        onChange('');
    };
    LocationPicker.prototype.handlePopOverClick = function (e) {
        e.stopPropagation();
        e.preventDefault();
    };
    LocationPicker.prototype.handleChange = function (value) {
        if (value) {
            value = __assign(__assign({}, value), { vendor: this.props.vendor });
        }
        this.props.onChange(value);
    };
    LocationPicker.prototype.handleTempChange = function (value) {
        if (value) {
            value = __assign(__assign({}, value), { vendor: this.props.vendor });
        }
        this.tempValue = value;
    };
    LocationPicker.prototype.handleConfirm = function () {
        this.props.onChange(this.tempValue);
        this.close();
    };
    LocationPicker.prototype.render = function () {
        var _this = this;
        var _a;
        var _b = this.props, cx = _b.classnames, value = _b.value, className = _b.className, popoverClassName = _b.popoverClassName, disabled = _b.disabled, placeholder = _b.placeholder, getLocationPlaceholder = _b.getLocationPlaceholder, clearable = _b.clearable, popOverContainer = _b.popOverContainer, vendor = _b.vendor, coordinatesType = _b.coordinatesType, ak = _b.ak, mobileUI = _b.mobileUI, autoSelectCurrentLoc = _b.autoSelectCurrentLoc, onlySelectCurrentLoc = _b.onlySelectCurrentLoc, _c = _b.hideViewControl, hideViewControl = _c === void 0 ? false : _c;
        var __ = this.props.translate;
        var _d = this.state, isFocused = _d.isFocused, isOpened = _d.isOpened;
        ((function () {
            switch (vendor) {
                case 'baidu':
                    return (React__default.createElement(BaiduMapPicker, { ak: ak, value: value, placeholder: __('MapPicker.searchPlaceholder'), coordinatesType: coordinatesType, autoSelectCurrentLoc: autoSelectCurrentLoc, onlySelectCurrentLoc: onlySelectCurrentLoc, onChange: _this.handleChange, hideViewControl: hideViewControl }));
                case 'gaode':
                    return (React__default.createElement(GaodeMapPicker, { ak: ak, value: value, coordinatesType: coordinatesType, onChange: _this.handleChange }));
                default:
                    return React__default.createElement(Alert2, null, __("{{vendor}} \u5730\u56FE\u63A7\u4EF6\u4E0D\u652F\u6301", { vendor: vendor }));
            }
        }))();
        return (React__default.createElement("div", { tabIndex: 0, onKeyPress: this.handleKeyPress, onFocus: this.handleFocus, onBlur: this.handleBlur, className: cx("LocationPicker", {
                'is-mobile': mobileUI,
                'is-disabled': disabled,
                'is-focused': isFocused,
                'is-active': isOpened
            }, className), ref: this.domRef, onClick: this.handleClick },
            value ? (React__default.createElement("span", { className: cx('LocationPicker-value') }, value.address)) : (React__default.createElement("span", { className: cx('LocationPicker-placeholder') }, __(onlySelectCurrentLoc ? getLocationPlaceholder : placeholder))),
            clearable && !disabled && value ? (React__default.createElement("a", { className: cx('LocationPicker-clear'), onClick: this.clearValue },
                React__default.createElement(Icon, { icon: "input-clear", className: "icon" }))) : null,
            React__default.createElement("a", { className: cx('LocationPicker-toggler') },
                React__default.createElement(Icon, { icon: "location", className: "icon" })),
            mobileUI ? (React__default.createElement(PopUp, { className: cx("LocationPicker-popup"), container: popOverContainer || this.getParent, isShow: isOpened, onHide: this.close, showConfirm: true, onConfirm: this.handleConfirm },
                React__default.createElement("div", { className: cx('LocationPicker-popup-inner') }, vendor === 'baidu' ? (React__default.createElement(BaiduMapPicker, { ak: ak, value: value, placeholder: __('MapPicker.searchPlaceholder'), coordinatesType: coordinatesType, autoSelectCurrentLoc: autoSelectCurrentLoc, onlySelectCurrentLoc: onlySelectCurrentLoc, onChange: this.handleTempChange, hideViewControl: hideViewControl })) : (React__default.createElement(Alert2, null, __('{{vendor}} 地图控件不支持', { vendor: vendor })))))) : (React__default.createElement(Overlay, { target: this.getTarget, container: popOverContainer || this.getParent, rootClose: false, show: isOpened },
                React__default.createElement(PopOver, { className: cx('LocationPicker-popover', popoverClassName), onHide: this.close, overlay: true, onClick: this.handlePopOverClick, style: { width: (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.offsetWidth } }, vendor === 'baidu' ? (React__default.createElement(BaiduMapPicker, { ak: ak, value: value, placeholder: __('MapPicker.searchPlaceholder'), coordinatesType: coordinatesType, autoSelectCurrentLoc: autoSelectCurrentLoc, onlySelectCurrentLoc: onlySelectCurrentLoc, onChange: this.handleChange, hideViewControl: hideViewControl })) : (React__default.createElement(Alert2, null, __('{{vendor}} 地图控件不支持', { vendor: vendor }))))))));
    };
    LocationPicker.defaultProps = {
        placeholder: 'LocationPicker.placeholder',
        getLocationPlaceholder: 'LocationPicker.getLocation',
        clearable: false
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleKeyPress", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleFocus", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleBlur", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleClick", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "getTarget", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "getParent", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Function]),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "open", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "close", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "clearValue", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handlePopOverClick", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleTempChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleConfirm", null);
    return LocationPicker;
}(React__default.Component));
var ThemedCity = themeable(localeable(LocationPicker));

export { LocationPicker, ThemedCity as default };
