"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Address", {
    enumerable: true,
    get: function() {
        return Address;
    }
});
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 _components = require("@tarojs/components");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _index = /*#__PURE__*/ _interop_require_default._(require("../popup/index"));
var _existRender = require("./existRender");
var _elevatorRender = require("./elevatorRender");
var _cascaderRender = require("./cascaderRender");
var _index1 = require("../configprovider/index");
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var _mergeprops = require("../../utils/merge-props");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    defaultValue: [],
    type: 'cascader',
    options: [],
    optionKey: {
        textKey: 'text',
        valueKey: 'value',
        childrenKey: 'children'
    },
    format: {},
    custom: false,
    existList: [],
    hotList: [],
    height: '200px',
    defaultIcon: null,
    selectIcon: null,
    closeIcon: null,
    backIcon: null
});
var InternalAddress = function InternalAddress(props, ref) {
    var locale = (0, _index1.useConfig)().locale;
    var _mergeProps = (0, _mergeprops.mergeProps)(defaultProps, props), style = _mergeProps.style, className = _mergeProps.className, visible = _mergeProps.visible, defaultVisible = _mergeProps.defaultVisible, defaultValue = _mergeProps.defaultValue, children = _mergeProps.children, type = _mergeProps.type, options = _mergeProps.options, optionKey = _mergeProps.optionKey, format = _mergeProps.format, height = _mergeProps.height, title = _mergeProps.title, existList = _mergeProps.existList, custom = _mergeProps.custom, selectIcon = _mergeProps.selectIcon, defaultIcon = _mergeProps.defaultIcon, closeIcon = _mergeProps.closeIcon, backIcon = _mergeProps.backIcon, hotList = _mergeProps.hotList, onChange = _mergeProps.onChange, onExistSelect = _mergeProps.onExistSelect, onClose = _mergeProps.onClose, onSwitch = _mergeProps.onSwitch, rest = (0, _object_without_properties._)(_mergeProps, [
        "style",
        "className",
        "visible",
        "defaultVisible",
        "defaultValue",
        "children",
        "type",
        "options",
        "optionKey",
        "format",
        "height",
        "title",
        "existList",
        "custom",
        "selectIcon",
        "defaultIcon",
        "closeIcon",
        "backIcon",
        "hotList",
        "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 renderLeftOnCascaderSwitch = function renderLeftOnCascaderSwitch() {
        if (!custom) return null;
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-left-icon"),
            onClick: onSwitchModule
        }, /*#__PURE__*/ _react.default.isValidElement(backIcon) ? backIcon : /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.ArrowLeft, {
            color: "#cccccc"
        }));
    };
    var selectedExistItem = function selectedExistItem(data) {
        onExistSelect && onExistSelect(data);
        handleClose();
    };
    var onSwitchModule = function onSwitchModule() {
        var nextType = currentType === 'exist' ? 'cascader' : 'exist';
        setCurrentType(nextType);
        onSwitch && onSwitch({
            type: nextType
        });
    };
    var renderElevator = function renderElevator() {
        return /*#__PURE__*/ _react.default.createElement(_elevatorRender.ElevatorRender, {
            visible: innerVisible,
            closeable: true,
            title: title || locale.address.selectRegion,
            left: backIcon,
            defaultValue: defaultValue,
            closeIcon: closeIcon,
            options: options,
            hotList: hotList,
            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);
            }
        });
    };
    var renderCascator = function renderCascator() {
        return /*#__PURE__*/ _react.default.createElement(_cascaderRender.CascaderRender, {
            visible: innerVisible,
            closeable: true,
            title: title || locale.address.selectRegion,
            left: renderLeftOnCascaderSwitch(),
            defaultValue: defaultValue,
            closeIcon: closeIcon,
            options: options,
            format: format,
            optionKey: optionKey,
            type: currentType,
            height: height,
            onClose: handleClose,
            onChange: function onChange1(val, params) {
                onChange && onChange(val, params);
            }
        });
    };
    var renderExist = function renderExist() {
        return /*#__PURE__*/ _react.default.createElement(_index.default, {
            visible: innerVisible,
            position: "bottom",
            round: true,
            closeable: true,
            closeIcon: closeIcon,
            title: title || locale.address.selectRegion,
            onClose: handleClose
        }, /*#__PURE__*/ _react.default.createElement(_components.View, {
            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
        })));
    };
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, currentType === 'elevator' ? renderElevator() : null, currentType === 'cascader' ? renderCascator() : null, currentType === 'exist' ? renderExist() : null);
};
var Address = /*#__PURE__*/ (0, _react.forwardRef)(InternalAddress);
Address.displayName = 'NutAddress';
