"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _InputWithOptions = _interopRequireDefault(require("../InputWithOptions"));
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _Input = _interopRequireDefault(require("../Input"));
var _Box = _interopRequireDefault(require("../Box"));
var _Loader = _interopRequireDefault(require("../Loader"));
var _Text = _interopRequireDefault(require("../Text"));
var _AddressInput = require("./AddressInput.constants");
var _WixDesignSystemIconThemeProvider = require("../WixDesignSystemIconThemeProvider");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/AddressInput/AddressInput.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var AddressInput = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
  var {
    dataHook,
    className,
    size,
    clearButton = true,
    clearButtonAriaLabel,
    placeholder,
    disabled,
    onFocus,
    onBlur,
    autoSelect,
    statusMessage,
    border = 'round',
    onManuallyInput,
    popoverProps,
    initialValue = '',
    onChange,
    onSelect,
    onClear,
    value: controlledValue,
    status,
    options,
    noResultsText
  } = _ref;
  var icons = (0, _WixDesignSystemIconThemeProvider.useIcons)('AddressInput', {
    Search: _wixUiIconsCommon.Search
  });
  var [inputValue, setInputValue] = (0, _react.useState)(initialValue);
  var [isDropdownOpen, setIsDropdownOpen] = (0, _react.useState)(false);
  var innerRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, () => ({
    focus: options => {
      var _innerRef$current;
      (_innerRef$current = innerRef.current) == null || _innerRef$current.focus(options);
    }
  }));
  var handleChange = event => {
    setInputValue(event.target.value);
    onChange == null || onChange(event);
  };
  var handleSelect = option => {
    setInputValue(option.label);
    onSelect == null || onSelect(option);
  };
  var handleClear = () => {
    setInputValue('');
    onClear == null || onClear();
  };
  var getValue = (0, _react.useCallback)(() => {
    return controlledValue !== undefined ? controlledValue : inputValue;
  }, [controlledValue, inputValue]);
  var isLoading = status === 'loading';
  var getStatus = () => {
    if (isLoading && isDropdownOpen) {
      return undefined;
    }
    return status;
  };
  var loadingOption = (0, _react.useMemo)(() => ({
    id: '',
    disabled: true,
    value: () => /*#__PURE__*/_react.default.createElement(_Box.default, {
      flex: 1,
      align: "center",
      padding: "3px",
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 99,
        columnNumber: 11
      }
    }, /*#__PURE__*/_react.default.createElement(_Loader.default, {
      size: "tiny",
      dataHook: _AddressInput.dataHooks.loader,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 100,
        columnNumber: 13
      }
    }))
  }), []);
  var noResultsOption = (0, _react.useMemo)(() => {
    var isString = typeof noResultsText === 'string';
    var value = isString ? /*#__PURE__*/_react.default.createElement(_Text.default, {
      light: true,
      secondary: true,
      dataHook: _AddressInput.dataHooks.noResultsText,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 110,
        columnNumber: 9
      }
    }, noResultsText) : noResultsText;
    return {
      id: 'no-results',
      disabled: true,
      overrideOptionStyle: !isString,
      value
    };
  }, [noResultsText]);
  var renderedOptions = (0, _react.useMemo)(() => {
    var value = getValue();
    var noResultsFound = !options || options.length === 0;
    if (isLoading) {
      return [loadingOption];
    }
    if (value && noResultsFound && noResultsText) {
      return [noResultsOption];
    }
    return options || [];
  }, [isLoading, options, noResultsOption, loadingOption, noResultsText, getValue]);
  return /*#__PURE__*/_react.default.createElement(_InputWithOptions.default, {
    dataHook: dataHook,
    className: className,
    clearButton: clearButton,
    clearButtonAriaLabel: clearButtonAriaLabel,
    onChange: handleChange,
    size: size,
    options: renderedOptions,
    onSelect: handleSelect,
    onManuallyInput: onManuallyInput,
    popoverProps: popoverProps,
    value: getValue(),
    disabled: disabled,
    border: border,
    onClear: clearButton ? handleClear : undefined,
    onFocus: onFocus,
    onBlur: onBlur,
    autoSelect: autoSelect,
    status: getStatus(),
    statusMessage: statusMessage,
    menuArrow: false,
    highlight: true,
    prefix: /*#__PURE__*/_react.default.createElement(_Input.default.IconAffix, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 171,
        columnNumber: 11
      }
    }, /*#__PURE__*/_react.default.createElement(icons.Search, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 172,
        columnNumber: 13
      }
    })),
    placeholder: placeholder,
    onOptionsShow: () => setIsDropdownOpen(true),
    onOptionsHide: () => setIsDropdownOpen(false),
    autocomplete: "off",
    ref: innerRef,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 148,
      columnNumber: 7
    }
  });
});
AddressInput.displayName = 'AddressInput';
var _default = exports.default = AddressInput;
//# sourceMappingURL=AddressInput.js.map