/**
 * amis-ui v6.12.0-beta.0
 * 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 icons = require('./icons.js');
var Alert2 = require('./Alert2.js');
var BaiduMapPicker = require('./BaiduMapPicker.js');
var GaodeMapPicker = require('./GaodeMapPicker.js');
var PopUp = require('./PopUp.js');

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

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

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var LocationPicker = /** @class */ (function (_super) {
    tslib.__extends(LocationPicker, _super);
    function LocationPicker() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.domRef = React__default["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 = tslib.__assign(tslib.__assign({}, value), { vendor: this.props.vendor });
        }
        this.props.onChange(value);
    };
    LocationPicker.prototype.handleTempChange = function (value) {
        if (value) {
            value = tslib.__assign(tslib.__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 (_J$X_(BaiduMapPicker["default"], { ak: ak, value: value, coordinatesType: coordinatesType, autoSelectCurrentLoc: autoSelectCurrentLoc, onlySelectCurrentLoc: onlySelectCurrentLoc, onChange: _this.handleChange, hideViewControl: hideViewControl }));
                case 'gaode':
                    return (_J$X_(GaodeMapPicker["default"], { ak: ak, value: value, coordinatesType: coordinatesType, onChange: _this.handleChange }));
                default:
                    return _J$X_(Alert2["default"], null, __("{{vendor}} \u5730\u56FE\u63A7\u4EF6\u4E0D\u652F\u6301", { vendor: vendor }));
            }
        }))();
        return (_J$X_("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 ? (_J$X_("span", { className: cx('LocationPicker-value') }, value.address)) : (_J$X_("span", { className: cx('LocationPicker-placeholder') }, __(onlySelectCurrentLoc ? getLocationPlaceholder : placeholder))),
            clearable && !disabled && value ? (_J$X_("a", { className: cx('LocationPicker-clear'), onClick: this.clearValue },
                _J$X_(icons.Icon, { icon: "input-clear", className: "icon" }))) : null,
            _J$X_("a", { className: cx('LocationPicker-toggler') },
                _J$X_(icons.Icon, { icon: "location", className: "icon" })),
            mobileUI ? (_J$X_(PopUp["default"], { className: cx("LocationPicker-popup"), container: popOverContainer || this.getParent, isShow: isOpened, onHide: this.close, showConfirm: true, onConfirm: this.handleConfirm },
                _J$X_("div", { className: cx('LocationPicker-popup-inner') }, vendor === 'baidu' ? (_J$X_(BaiduMapPicker["default"], { ak: ak, value: value, coordinatesType: coordinatesType, autoSelectCurrentLoc: autoSelectCurrentLoc, onlySelectCurrentLoc: onlySelectCurrentLoc, onChange: this.handleTempChange, hideViewControl: hideViewControl })) : (_J$X_(Alert2["default"], null, __('{{vendor}} 地图控件不支持', { vendor: vendor })))))) : (_J$X_(amisCore.Overlay, { target: this.getTarget, container: popOverContainer || this.getParent, rootClose: false, show: isOpened },
                _J$X_(amisCore.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' ? (_J$X_(BaiduMapPicker["default"], { ak: ak, value: value, coordinatesType: coordinatesType, autoSelectCurrentLoc: autoSelectCurrentLoc, onlySelectCurrentLoc: onlySelectCurrentLoc, onChange: this.handleChange, hideViewControl: hideViewControl })) : (_J$X_(Alert2["default"], null, __('{{vendor}} 地图控件不支持', { vendor: vendor }))))))));
    };
    LocationPicker.defaultProps = {
        placeholder: 'LocationPicker.placeholder',
        getLocationPlaceholder: 'LocationPicker.getLocation',
        clearable: false
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleKeyPress", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleFocus", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleBlur", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "getTarget", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "getParent", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Function]),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "open", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "close", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "clearValue", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handlePopOverClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleTempChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], LocationPicker.prototype, "handleConfirm", null);
    return LocationPicker;
}(React__default["default"].Component));
var ThemedCity = amisCore.themeable(amisCore.localeable(LocationPicker));

exports.LocationPicker = LocationPicker;
exports["default"] = ThemedCity;
