/**
 * 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 Picker = require('./Picker.js');
var ResultBox = require('./ResultBox.js');
var useSetState = require('../hooks/use-set-state.js');
var useUpdateEffect = require('../hooks/use-update-effect.js');
require('lodash/pick');
var amisCore = require('amis-core');
var PopUp = require('./PopUp.js');

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var CityArea = React.memo(function (props) {
    var _a;
    var _b = props.joinValues, joinValues = _b === void 0 ? true : _b, _c = props.extractValue, extractValue = _c === void 0 ? true : _c, _d = props.delimiter, delimiter = _d === void 0 ? ',' : _d, _e = props.allowCity, allowCity = _e === void 0 ? true : _e, _f = props.allowDistrict, allowDistrict = _f === void 0 ? true : _f, _g = props.allowStreet, allowStreet = _g === void 0 ? false : _g, 
    // 默认北京东城区
    _h = props.value, 
    // 默认北京东城区
    value = _h === void 0 ? 110101 : _h, cx = props.classnames, style = props.style, __ = props.translate, _j = props.disabled, disabled = _j === void 0 ? false : _j, popOverContainer = props.popOverContainer, mobileUI = props.mobileUI;
    var _k = tslib.__read(React.useState([]), 2), values = _k[0], setValues = _k[1];
    var _l = tslib.__read(React.useState(''), 2), street = _l[0], setStreet = _l[1];
    var _m = tslib.__read(React.useState(), 2), confirmValues = _m[0], setConfirmValues = _m[1];
    var _o = tslib.__read(useSetState["default"](), 2), db = _o[0], updateDb = _o[1];
    var _p = tslib.__read(useSetState["default"]({
        columns: []
    }), 2), state = _p[0], updateState = _p[1];
    var _q = tslib.__read(React.useState(false), 2), isOpened = _q[0], setIsOpened = _q[1];
    var onChange = function (columnValues, columnIndex) {
        var _a, _b, _c, _d, _e;
        // 清空后面的值
        while (columnValues[columnIndex++]) {
            columnValues[columnIndex++] = -1;
        }
        var _f = tslib.__read(columnValues, 3), provience = _f[0], city = _f[1], district = _f[2];
        if (city === -1) {
            city = (_b = (_a = db.city) === null || _a === void 0 ? void 0 : _a[provience]) === null || _b === void 0 ? void 0 : _b[0];
        }
        if (district === -1) {
            district = (_e = (_d = (_c = db.district) === null || _c === void 0 ? void 0 : _c[provience]) === null || _d === void 0 ? void 0 : _d[city]) === null || _e === void 0 ? void 0 : _e[0];
        }
        var tempValues = [provience, city, district];
        if (!allowDistrict) {
            tempValues.splice(2, 1);
        }
        if (!allowCity) {
            tempValues.splice(1, 1);
        }
        setValues(tempValues);
    };
    var propsChange = function () {
        var onChange = props.onChange;
        var _a = tslib.__read(values, 3), province = _a[0], city = _a[1], district = _a[2];
        var code = allowDistrict && district
            ? district
            : allowCity && city
                ? city
                : province;
        if (typeof extractValue === 'undefined' ? joinValues : extractValue) {
            code
                ? onChange(allowStreet && street
                    ? [code, street].join(delimiter)
                    : String(code))
                : onChange('');
        }
        else {
            onChange({
                code: code,
                provinceCode: province,
                province: db[province],
                cityCode: city,
                city: db[city],
                districtCode: district,
                district: db[district],
                street: street
            });
        }
    };
    var onConfirm = function () {
        var confirmValues = values.map(function (item) { return ({
            text: db[item],
            value: item
        }); });
        setConfirmValues(confirmValues);
        propsChange();
        setIsOpened(false);
    };
    var onCancel = function () {
        setIsOpened(false);
        if (props.onCancel)
            props.onCancel();
    };
    var getPropsValue = function () {
        var _a;
        // 最后一项的值、默认北京东城区
        var code = (value && value.code) ||
            (typeof value === 'number' && value) ||
            (typeof value === 'string' && /(\d{6})/.test(value) && RegExp.$1) ||
            110101;
        var values = [];
        if (code && db[code]) {
            code = parseInt(code, 10);
            var provinceCode = code - (code % 10000);
            var cityCode = code - (code % 100);
            if (db[provinceCode]) {
                values[0] = provinceCode;
            }
            if (db[cityCode] && allowCity) {
                values[1] = cityCode;
            }
            else if (~((_a = db.city[provinceCode]) === null || _a === void 0 ? void 0 : _a.indexOf(code)) && allowCity) {
                values[1] = code;
            }
            if (code % 100 && allowDistrict) {
                values[2] = code;
            }
            setValues(values);
            if (props.value) {
                var confirmValues_1 = values.map(function (item) { return ({
                    text: db[item],
                    value: item
                }); });
                setConfirmValues(confirmValues_1);
            }
        }
    };
    var updateColumns = function () {
        if (!db) {
            return;
        }
        var _a = tslib.__read(values, 3), provience = _a[0], city = _a[1], district = _a[2];
        var provienceColumn = db.province.map(function (code) {
            return { text: db[code], value: code, disabled: disabled };
        });
        var cityColumn = city
            ? db.city[provience].map(function (code) {
                return { text: db[code], value: code, disabled: disabled };
            })
            : [];
        var districtColumn = city && district
            ? db.district[provience][city].map(function (code) {
                return { text: db[code], value: code, disabled: disabled };
            })
            : [];
        var columns = [
            { options: provienceColumn },
            { options: cityColumn },
            { options: districtColumn }
        ];
        if (!allowDistrict || !allowCity) {
            columns.splice(2, 1);
        }
        if (!allowCity) {
            columns.splice(1, 1);
        }
        updateState({ columns: columns });
    };
    var loadDb = function () {
        Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['./CityDB.js', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})}).then(function (db) {
            updateDb(tslib.__assign(tslib.__assign({}, db.default), { province: db.province, city: db.city, district: db.district }));
        });
    };
    React.useEffect(function () {
        loadDb();
    }, []);
    React.useEffect(function () {
        db && (props.value || isOpened) && getPropsValue();
    }, [db, isOpened, props.value]);
    React.useEffect(function () {
        street && propsChange();
    }, [street]);
    useUpdateEffect["default"](function () {
        values.length && updateColumns();
    }, [values]);
    var result = (_a = confirmValues === null || confirmValues === void 0 ? void 0 : confirmValues.filter(function (item) { return item === null || item === void 0 ? void 0 : item.value; })) === null || _a === void 0 ? void 0 : _a.map(function (item) { return item.text; }).join(delimiter);
    return (_J$X_("div", { className: cx("CityArea"), style: style },
        _J$X_(ResultBox["default"], { className: cx('CityArea-Input', isOpened ? 'is-active' : ''), allowInput: false, result: result, onResultChange: function () { }, onResultClick: function () { return setIsOpened(!isOpened); }, placeholder: __('Select.placeholder'), mobileUI: mobileUI, disabled: disabled }),
        allowStreet && values[0] ? (_J$X_("input", { className: cx('CityArea-Input'), value: street, onChange: function (e) {
                return setStreet(e.currentTarget.value);
            }, placeholder: __('City.street'), disabled: disabled, readOnly: true })) : null,
        _J$X_(PopUp["default"], { className: cx("CityArea-popup"), container: popOverContainer, isShow: isOpened, showConfirm: true, onConfirm: onConfirm, onHide: onCancel },
            _J$X_(Picker["default"], { className: 'CityArea-picker', columns: state.columns, onChange: onChange, showToolbar: false, labelField: "text", itemHeight: 40, value: values, classnames: props.classnames, classPrefix: props.classPrefix }))));
});
var CityArea$1 = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(CityArea, {
    value: 'onChange'
})));

exports["default"] = CityArea$1;
