import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/esm/AtlasAddressInput/test/AtlasAddressInput.spec.jsx",
  _this = this;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
import { AmbassadorTestkit } from '@wix/ambassador-testkit';
import { WixAtlasServiceWeb } from '@wix/ambassador-wix-atlas-service-web/http';
import { AmbassadorHTTPError } from '@wix/ambassador/runtime/http';
import { aCommonAddress, aPredictResponse, aSearchResponse, aSearchResult, aV2Prediction as aPrediction } from '@wix/ambassador-wix-atlas-service-web/builders';
import { createRendererWithUniDriver, cleanup, act, waitFor } from '../../utils/test-utils/unit';
import AtlasAddressInput from '../AtlasAddressInput';
import { atlasAddressInputPrivateDriverFactory } from './AtlasAddressInput.private.uni.driver';
import { BASE_ATLAS_URL } from '../../providers/useAtlasClient/useAtlasClient';
var commonProps = {
  debounceMs: 0
};
var mockResults = function mockResults(ambassadorTestkit, amountOfItems) {
  var predictions = Array.from({
    length: amountOfItems
  }, function (_, index) {
    var mainText = "Address ".concat(index + 1);
    var secondaryText = 'Country';
    var description = "".concat(mainText, ", ").concat(secondaryText);
    return aPrediction().withSearchId("".concat(index)).withDescription(description).withTextStructure({
      mainText: mainText,
      secondaryText: secondaryText
    }).build();
  });
  var response = aPredictResponse().withPredictions(predictions).build();
  var atlasStub = ambassadorTestkit.createStub(WixAtlasServiceWeb, BASE_ATLAS_URL);
  atlasStub.AutocompleteServiceV2().predict.always().resolve(response);
  return predictions;
};
var mockSearchAddresses = function mockSearchAddresses(ambassadorTestkit) {
  var amountOfItems = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
  var addresses = Array.from({
    length: amountOfItems
  }, function (_, index) {
    var mainText = "Address ".concat(index + 1);
    var secondaryText = 'Country';
    var formattedAddress = "".concat(mainText, ", ").concat(secondaryText);
    return aCommonAddress().withFormattedAddress(formattedAddress).build();
  });
  var searchResults = addresses.map(function (address) {
    return aSearchResult().withAddress(address).build();
  });
  var response = aSearchResponse().withSearchResults(searchResults).build();
  var atlasStub = ambassadorTestkit.createStub(WixAtlasServiceWeb, BASE_ATLAS_URL);
  atlasStub.LocationServiceV2().search.always().resolve(response);
  return addresses;
};
var mockAmbassadorError = function mockAmbassadorError(ambassadorTestkit) {
  var atlasStub = ambassadorTestkit.createStub(WixAtlasServiceWeb, BASE_ATLAS_URL);
  atlasStub.AutocompleteServiceV2().predict.always().reject();
};
describe(AtlasAddressInput.displayName, function () {
  var consoleWarnMock = vi.spyOn(console, 'log').mockImplementation();
  var ambassadorTestkit = new AmbassadorTestkit();
  ambassadorTestkit.beforeAndAfter();
  var render = createRendererWithUniDriver(atlasAddressInputPrivateDriverFactory);
  afterAll(function () {
    consoleWarnMock.mockRestore();
  });
  afterEach(cleanup);
  it('should render', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var _render, driver;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _render = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, commonProps, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 93,
              columnNumber: 31
            }
          }))), driver = _render.driver;
          _context.t0 = expect;
          _context.next = 4;
          return driver.exists();
        case 4:
          _context.t1 = _context.sent;
          (0, _context.t0)(_context.t1).toBe(true);
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })));
  it('should fetch results from Atlas on enter text', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
    var amountOfItems, _render2, driver;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          amountOfItems = 5;
          mockResults(ambassadorTestkit, amountOfItems);
          _render2 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, commonProps, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 102,
              columnNumber: 31
            }
          }))), driver = _render2.driver;
          _context4.t0 = expect;
          _context4.next = 6;
          return driver.getAmountOfItems();
        case 6:
          _context4.t1 = _context4.sent;
          (0, _context4.t0)(_context4.t1).toBe(0);
          _context4.next = 10;
          return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
            return _regeneratorRuntime.wrap(function _callee2$(_context2) {
              while (1) switch (_context2.prev = _context2.next) {
                case 0:
                  _context2.next = 2;
                  return driver.enterText('test');
                case 2:
                case "end":
                  return _context2.stop();
              }
            }, _callee2);
          })));
        case 10:
          _context4.next = 12;
          return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
            return _regeneratorRuntime.wrap(function _callee3$(_context3) {
              while (1) switch (_context3.prev = _context3.next) {
                case 0:
                  _context3.t0 = expect;
                  _context3.next = 3;
                  return driver.getAmountOfItems();
                case 3:
                  _context3.t1 = _context3.sent;
                  (0, _context3.t0)(_context3.t1).toBe(amountOfItems);
                case 5:
                case "end":
                  return _context3.stop();
              }
            }, _callee3);
          })));
        case 12:
          _context4.t2 = expect;
          _context4.next = 15;
          return driver.getItemMainLabelAt(0);
        case 15:
          _context4.t3 = _context4.sent;
          (0, _context4.t2)(_context4.t3).toBe('Address 1');
          _context4.t4 = expect;
          _context4.next = 20;
          return driver.getItemSecondaryLabelAt(0);
        case 20:
          _context4.t5 = _context4.sent;
          (0, _context4.t4)(_context4.t5).toBe('Country');
        case 22:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  })));
  it('should invoke onChange', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
    var text, props, _render3, driver;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          text = 'test';
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            onChange: vi.fn()
          });
          _render3 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 122,
              columnNumber: 31
            }
          }))), driver = _render3.driver;
          expect(props.onChange).not.toHaveBeenCalled();
          _context5.t0 = expect;
          _context5.next = 7;
          return driver.getInputValue();
        case 7:
          _context5.t1 = _context5.sent;
          (0, _context5.t0)(_context5.t1).toEqual('');
          _context5.next = 11;
          return driver.enterText(text);
        case 11:
          expect(props.onChange).toHaveBeenCalled();
          _context5.t2 = expect;
          _context5.next = 15;
          return driver.getInputValue();
        case 15:
          _context5.t3 = _context5.sent;
          (0, _context5.t2)(_context5.t3).toEqual(text);
        case 17:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  })));
  it('should invoke onClear', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
    var props, _render4, driver;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) switch (_context6.prev = _context6.next) {
        case 0:
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            onClear: vi.fn(),
            initialValue: 'test'
          });
          _render4 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 139,
              columnNumber: 31
            }
          }))), driver = _render4.driver;
          expect(props.onClear).not.toHaveBeenCalled();
          _context6.t0 = expect;
          _context6.next = 6;
          return driver.getInputValue();
        case 6:
          _context6.t1 = _context6.sent;
          (0, _context6.t0)(_context6.t1).toEqual('test');
          _context6.next = 10;
          return driver.clickClear();
        case 10:
          expect(props.onClear).toHaveBeenCalled();
          _context6.t2 = expect;
          _context6.next = 14;
          return driver.getInputValue();
        case 14:
          _context6.t3 = _context6.sent;
          (0, _context6.t2)(_context6.t3).toEqual('');
        case 16:
        case "end":
          return _context6.stop();
      }
    }, _callee6);
  })));
  it('should invoke onSelect', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
    var amountOfItems, predictions, props, _render5, driver;
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) switch (_context8.prev = _context8.next) {
        case 0:
          amountOfItems = 5;
          predictions = mockResults(ambassadorTestkit, amountOfItems);
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            onSelect: vi.fn(function (option, getAddress) {})
          });
          _render5 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 157,
              columnNumber: 31
            }
          }))), driver = _render5.driver;
          expect(props.onSelect).not.toHaveBeenCalled();
          _context8.next = 7;
          return driver.enterText('test');
        case 7:
          _context8.next = 9;
          return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
            return _regeneratorRuntime.wrap(function _callee7$(_context7) {
              while (1) switch (_context7.prev = _context7.next) {
                case 0:
                  _context7.t0 = expect;
                  _context7.next = 3;
                  return driver.getAmountOfItems();
                case 3:
                  _context7.t1 = _context7.sent;
                  (0, _context7.t0)(_context7.t1).toBe(amountOfItems);
                case 5:
                case "end":
                  return _context7.stop();
              }
            }, _callee7);
          })));
        case 9:
          _context8.next = 11;
          return driver.clickAtOption(0);
        case 11:
          expect(props.onSelect).toHaveBeenCalledWith(expect.objectContaining({
            id: predictions[0].searchId
          }), expect.any(Function));
        case 12:
        case "end":
          return _context8.stop();
      }
    }, _callee8);
  })));
  it('should invoke onSelect with search result when `selectOnSubmit` is true', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee1() {
    var addresses, getAddress, props, _render6, driver, testValue;
    return _regeneratorRuntime.wrap(function _callee1$(_context1) {
      while (1) switch (_context1.prev = _context1.next) {
        case 0:
          addresses = mockSearchAddresses(ambassadorTestkit);
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            onSelect: vi.fn(/*#__PURE__*/function () {
              var _ref0 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9(option, getAddressFn) {
                return _regeneratorRuntime.wrap(function _callee9$(_context9) {
                  while (1) switch (_context9.prev = _context9.next) {
                    case 0:
                      // set getAddress to the resulting function
                      getAddress = getAddressFn;
                    case 1:
                    case "end":
                      return _context9.stop();
                  }
                }, _callee9);
              }));
              return function (_x, _x2) {
                return _ref0.apply(this, arguments);
              };
            }()),
            selectOnSubmit: true
          });
          _render6 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 184,
              columnNumber: 31
            }
          }))), driver = _render6.driver;
          testValue = 'test';
          _context1.next = 6;
          return driver.enterText(testValue);
        case 6:
          expect(props.onSelect).not.toHaveBeenCalled();
          _context1.next = 9;
          return driver.pressEnter();
        case 9:
          expect(props.onSelect).toHaveBeenCalledWith(expect.objectContaining({
            label: testValue
          }), expect.any(Function));
          _context1.next = 12;
          return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee0() {
            return _regeneratorRuntime.wrap(function _callee0$(_context0) {
              while (1) switch (_context0.prev = _context0.next) {
                case 0:
                  _context0.t0 = expect;
                  _context0.next = 3;
                  return getAddress();
                case 3:
                  _context0.t1 = _context0.sent;
                  (0, _context0.t0)(_context0.t1).toEqual(addresses[0]);
                case 5:
                case "end":
                  return _context0.stop();
              }
            }, _callee0);
          })));
        case 12:
        case "end":
          return _context1.stop();
      }
    }, _callee1);
  })));
  it('should display option prefix when prop is passed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee11() {
    var amountOfItems, optionPrefix, props, _render7, driver, itemPrefix;
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) switch (_context11.prev = _context11.next) {
        case 0:
          amountOfItems = 1;
          mockResults(ambassadorTestkit, amountOfItems);
          optionPrefix = 'hello';
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            optionPrefix: optionPrefix
          });
          _render7 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 212,
              columnNumber: 31
            }
          }))), driver = _render7.driver;
          _context11.next = 7;
          return driver.enterText('test');
        case 7:
          _context11.next = 9;
          return driver.getItemPrefixAt(0);
        case 9:
          itemPrefix = _context11.sent;
          _context11.next = 12;
          return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
            return _regeneratorRuntime.wrap(function _callee10$(_context10) {
              while (1) switch (_context10.prev = _context10.next) {
                case 0:
                  _context10.t0 = expect;
                  _context10.next = 3;
                  return itemPrefix.text();
                case 3:
                  _context10.t1 = _context10.sent;
                  (0, _context10.t0)(_context10.t1).toBe(optionPrefix);
                case 5:
                case "end":
                  return _context10.stop();
              }
            }, _callee10);
          })));
        case 12:
        case "end":
          return _context11.stop();
      }
    }, _callee11);
  })));
  it('should display option suffix when prop is passed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee13() {
    var amountOfItems, optionSuffix, props, _render8, driver;
    return _regeneratorRuntime.wrap(function _callee13$(_context13) {
      while (1) switch (_context13.prev = _context13.next) {
        case 0:
          amountOfItems = 1;
          mockResults(ambassadorTestkit, amountOfItems);
          optionSuffix = 'hello';
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            optionSuffix: optionSuffix
          });
          _render8 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 231,
              columnNumber: 31
            }
          }))), driver = _render8.driver;
          _context13.next = 7;
          return driver.enterText('test');
        case 7:
          _context13.next = 9;
          return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee12() {
            var itemPrefix;
            return _regeneratorRuntime.wrap(function _callee12$(_context12) {
              while (1) switch (_context12.prev = _context12.next) {
                case 0:
                  _context12.next = 2;
                  return driver.getItemSuffixAt(0);
                case 2:
                  itemPrefix = _context12.sent;
                  _context12.t0 = expect;
                  _context12.next = 6;
                  return itemPrefix.text();
                case 6:
                  _context12.t1 = _context12.sent;
                  (0, _context12.t0)(_context12.t1).toBe(optionSuffix);
                case 8:
                case "end":
                  return _context12.stop();
              }
            }, _callee12);
          })));
        case 9:
        case "end":
          return _context13.stop();
      }
    }, _callee13);
  })));
  it('should invoke onError when an error occurs', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee15() {
    var onError, props, _render9, driver;
    return _regeneratorRuntime.wrap(function _callee15$(_context15) {
      while (1) switch (_context15.prev = _context15.next) {
        case 0:
          mockAmbassadorError(ambassadorTestkit);
          onError = vi.fn();
          props = _objectSpread(_objectSpread({}, commonProps), {}, {
            onError: onError
          });
          _render9 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, _extends({}, props, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 246,
              columnNumber: 31
            }
          }))), driver = _render9.driver;
          _context15.next = 6;
          return driver.enterText('test');
        case 6:
          _context15.next = 8;
          return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee14() {
            return _regeneratorRuntime.wrap(function _callee14$(_context14) {
              while (1) switch (_context14.prev = _context14.next) {
                case 0:
                  expect(onError).toHaveBeenCalledWith(expect.any(AmbassadorHTTPError));
                case 1:
                case "end":
                  return _context14.stop();
              }
            }, _callee14);
          })));
        case 8:
        case "end":
          return _context15.stop();
      }
    }, _callee15);
  })));
  it('should be focused when focus method called', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee17() {
    var addressRef, _render0, driver;
    return _regeneratorRuntime.wrap(function _callee17$(_context17) {
      while (1) switch (_context17.prev = _context17.next) {
        case 0:
          addressRef = /*#__PURE__*/React.createRef();
          _render0 = render(/*#__PURE__*/React.createElement(AtlasAddressInput, {
            ref: addressRef,
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 257,
              columnNumber: 31
            }
          })), driver = _render0.driver;
          _context17.t0 = expect;
          _context17.next = 5;
          return driver.isFocused();
        case 5:
          _context17.t1 = _context17.sent;
          (0, _context17.t0)(_context17.t1).toBeFalsy();
          _context17.next = 9;
          return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee16() {
            return _regeneratorRuntime.wrap(function _callee16$(_context16) {
              while (1) switch (_context16.prev = _context16.next) {
                case 0:
                  addressRef.current.focus();
                case 1:
                case "end":
                  return _context16.stop();
              }
            }, _callee16);
          })));
        case 9:
          _context17.t2 = expect;
          _context17.next = 12;
          return driver.isFocused();
        case 12:
          _context17.t3 = _context17.sent;
          (0, _context17.t2)(_context17.t3).toBeTruthy();
        case 14:
        case "end":
          return _context17.stop();
      }
    }, _callee17);
  })));
});