"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    Address: function() {
        return Address;
    },
    InternalAddress: function() {
        return InternalAddress;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _iconsreact = require("@nutui/icons-react");
var _popup = /*#__PURE__*/ _interop_require_default._(require("../popup"));
var _customRender = require("./customRender");
var _existRender = require("./existRender");
var _configprovider = require("../configprovider");
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    defaultValue: [],
    type: 'custom',
    options: [],
    optionKey: {
        textKey: 'text',
        valueKey: 'value',
        childrenKey: 'children'
    },
    format: {},
    custom: false,
    existList: [],
    height: '200px',
    defaultIcon: null,
    selectIcon: null,
    closeIcon: null,
    backIcon: null
});
var InternalAddress = function InternalAddress(props, ref) {
    var locale = (0, _configprovider.useConfig)().locale;
    var _ref = (0, _object_spread._)({}, defaultProps, props), style = _ref.style, className = _ref.className, visible = _ref.visible, defaultVisible = _ref.defaultVisible, defaultValue = _ref.defaultValue, children = _ref.children, type = _ref.type, options = _ref.options, optionKey = _ref.optionKey, format = _ref.format, height = _ref.height, title = _ref.title, existList = _ref.existList, custom = _ref.custom, selectIcon = _ref.selectIcon, defaultIcon = _ref.defaultIcon, closeIcon = _ref.closeIcon, backIcon = _ref.backIcon, onChange = _ref.onChange, onExistSelect = _ref.onExistSelect, onClose = _ref.onClose, onSwitch = _ref.onSwitch, rest = (0, _object_without_properties._)(_ref, [
        "style",
        "className",
        "visible",
        "defaultVisible",
        "defaultValue",
        "children",
        "type",
        "options",
        "optionKey",
        "format",
        "height",
        "title",
        "existList",
        "custom",
        "selectIcon",
        "defaultIcon",
        "closeIcon",
        "backIcon",
        "onChange",
        "onExistSelect",
        "onClose",
        "onSwitch"
    ]);
    var classPrefix = 'nut-address';
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(type), 2), currentType = _useState[0], setCurrentType = _useState[1];
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: visible,
        defaultValue: defaultVisible,
        finalValue: defaultVisible
    }), 2), innerVisible = _usePropsValue[0], setInnerVisible = _usePropsValue[1];
    (0, _react.useImperativeHandle)(ref, function() {
        return {
            open: function open() {
                setInnerVisible(true);
            },
            close: function close() {
                setInnerVisible(false);
            }
        };
    });
    var handleClose = function handleClose() {
        setInnerVisible(false);
        onClose && onClose();
    };
    var renderLeftOnCustomSwitch = function renderLeftOnCustomSwitch() {
        if (custom) {
            return /*#__PURE__*/ _react.default.createElement("div", {
                className: "".concat(classPrefix, "-left-icon"),
                onClick: onSwitchModule
            }, /*#__PURE__*/ _react.default.isValidElement(backIcon) ? backIcon : /*#__PURE__*/ _react.default.createElement(_iconsreact.ArrowLeft, {
                color: "#cccccc"
            }));
        }
        return null;
    };
    var selectedExistItem = function selectedExistItem(data) {
        onExistSelect && onExistSelect(data);
        handleClose();
    };
    var onSwitchModule = function onSwitchModule() {
        if (currentType === 'exist') {
            setCurrentType('custom');
        } else {
            setCurrentType('exist');
        }
        onSwitch && onSwitch({
            type: currentType
        });
    };
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, currentType === 'custom' || currentType === 'custom2' ? /*#__PURE__*/ _react.default.createElement(_customRender.CustomRender, {
        visible: innerVisible,
        closeable: true,
        title: title || locale.address.selectRegion,
        left: renderLeftOnCustomSwitch(),
        defaultValue: defaultValue,
        closeIcon: closeIcon,
        options: options,
        format: format,
        optionKey: optionKey,
        type: currentType,
        height: height,
        onClose: handleClose,
        onChange: function onChange1(val, params) {
            onChange === null || onChange === void 0 ? void 0 : onChange(val, params);
        }
    }) : /*#__PURE__*/ _react.default.createElement(_popup.default, {
        visible: innerVisible,
        position: "bottom",
        round: true,
        closeable: true,
        closeIcon: closeIcon,
        title: title || locale.address.selectRegion,
        onClose: handleClose
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, " ").concat(className || ''),
        style: (0, _object_spread._)({}, style)
    }, /*#__PURE__*/ _react.default.createElement(_existRender.ExistRender, {
        type: currentType,
        existList: existList,
        selectIcon: selectIcon,
        defaultIcon: defaultIcon,
        custom: custom,
        onSelect: selectedExistItem,
        onSwitch: onSwitchModule
    }))));
};
var Address = /*#__PURE__*/ (0, _react.forwardRef)(InternalAddress);
Address.displayName = 'NutAddress';
