"use strict";

exports.__esModule = true;
exports.createBackend = createBackend;
exports.createOrder = createOrder;
exports.ordersAPIHttpClientMocks = void 0;
var _client = require("@wix/fe-essentials/http-client/testkit/client");
var _http = require("@wix/ambassador-ecom-v1-order/http");
var _types = require("@wix/ambassador-ecom-v1-order/types");
var _backend = require("../testkit/backend");
var _chance = require("chance");
function createOrder(chance) {
  return ({
    createdDate = chance.date({
      min: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000),
      max: new Date()
    }),
    updatedDate = createdDate,
    ...overrides
  } = {}, index) => ({
    id: chance.guid(),
    number: index * 1000,
    createdDate,
    updatedDate,
    paymentStatus: chance.pickone(Object.values(_types.PaymentStatus)),
    fulfillmentStatus: chance.pickone(Object.values(_types.FulfillmentStatus)),
    status: chance.pickone(Object.values(_types.OrderStatus)),
    buyerNote: chance.sentence(),
    ...overrides
  });
}
function createBackend() {
  const chance = new _chance.Chance();
  return new _backend.InMemoryBackend({
    total: 150,
    paginationMode: 'cursor',
    delay: {
      min: 100,
      max: 2000
    },
    createOne: index => createOrder(chance)({}, index),
    predicate: query => {
      const {
        search
      } = query;
      const filters = query.filters ?? {};
      const rgx = search ? new RegExp(search, 'i') : null;
      return item => {
        if (rgx != null && !rgx.test(item.buyerNote)) {
          return false;
        }
        return !(filters.paymentStatus && filters.paymentStatus.length > 0 && filters.paymentStatus.indexOf(item.paymentStatus) === -1);
      };
    },
    orderBy: query => {
      return [...(query.sort ?? [])];
    },
    itemKey: item => item.id
  });
}
const ordersAPIHttpClientMocks = () => {
  const ordersBackend = createBackend();
  return [(0, _client.whenRequest)(_http.queryOrders).reply(200, async data => {
    const {
      query: {
        cursorPaging: {
          limit,
          cursor
        }
      },
      search
    } = data;
    return ordersBackend.fetchData({
      page: 1,
      limit,
      cursor,
      search,
      filters: {}
    }).then(({
      items,
      total,
      cursor: newCursor
    }) => ({
      orders: items,
      metadata: {
        total,
        cursors: {
          next: newCursor
        }
      }
    }));
  }).persist()];
};
exports.ordersAPIHttpClientMocks = ordersAPIHttpClientMocks;
//# sourceMappingURL=OrdersMocks.js.map