import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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 fetchMock from 'fetch-mock/cjs/client';
import { ActionOperationStatus } from '@atlaskit/linking-types';
import { blocker, high, low, major, medium, mike, nidhin, profile, sasha, trivial } from '../../images';
export var ORS_ACTIONS_DISCOVERY_ENDPOINT = /\/gateway\/api\/object-resolver\/actions$/;
export var ORS_ACTIONS_EXECUTION_ENDPOINT = /\/gateway\/api\/object-resolver\/actions\/execute$/;
var numberOfLoads = 0;
export var mockActionsDiscovery = function mockActionsDiscovery(overrides) {
  fetchMock.post(ORS_ACTIONS_DISCOVERY_ENDPOINT, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var getMock;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          getMock = function _getMock() {
            var actions = [{
              integrationKey: 'jira',
              actionKey: 'atlassian:work-item:update:summary',
              fieldKey: 'summary',
              type: 'string'
            }, {
              integrationKey: 'jira',
              actionKey: 'atlassian:work-item:update:status',
              type: 'string',
              fieldKey: 'status',
              inputs: {
                status: {
                  type: 'string',
                  fetchAction: {
                    actionKey: 'atlassian:work-item:get:statuses',
                    integrationKey: 'jira',
                    fieldKey: 'status',
                    type: 'string',
                    inputs: {
                      issueId: {
                        type: 'string'
                      }
                    }
                  }
                }
              }
            }, {
              fieldKey: 'priority',
              integrationKey: 'jira',
              actionKey: 'atlassian:work-item:update:priority',
              type: 'string',
              inputs: {
                priority: {
                  type: 'string',
                  fetchAction: {
                    actionKey: 'atlassian:work-item:get:priorities',
                    integrationKey: 'jira',
                    fieldKey: 'priority',
                    type: 'string',
                    inputs: {
                      projectId: {
                        type: 'string'
                      }
                    }
                  }
                }
              }
            }, {
              integrationKey: 'jira',
              actionKey: 'atlassian:work-item:update:assignee',
              type: 'string',
              fieldKey: 'assignee',
              inputs: {
                assignee: {
                  type: 'string',
                  fetchAction: {
                    actionKey: 'atlassian:work-item:get:assignees',
                    integrationKey: 'jira',
                    fieldKey: 'assignee',
                    type: 'string',
                    inputs: {
                      query: {
                        type: 'string'
                      }
                    }
                  }
                }
              }
            }];
            return _objectSpread({
              actions: actions,
              permissions: {
                data: new Array(20).fill(null).flatMap(function (_, i) {
                  return [{
                    ari: "ari:cloud:jira:DUMMY-158c8204-ff3b-47c2-adbb-a0906ccc722b:issue/".concat(i * 10 + numberOfLoads),
                    fieldKey: 'summary',
                    isEditable: i % 2 === 1
                  }, {
                    ari: "ari:cloud:jira:DUMMY-158c8204-ff3b-47c2-adbb-a0906ccc722b:issue/".concat(i * 10 + numberOfLoads),
                    fieldKey: 'status',
                    isEditable: i % 2 === 1
                  }, {
                    ari: "ari:cloud:jira:DUMMY-158c8204-ff3b-47c2-adbb-a0906ccc722b:issue/".concat(i * 10 + numberOfLoads),
                    fieldKey: 'priority',
                    isEditable: i % 2 === 1
                  }, {
                    ari: "ari:cloud:jira:DUMMY-158c8204-ff3b-47c2-adbb-a0906ccc722b:issue/".concat(i * 10 + numberOfLoads),
                    fieldKey: 'assignee',
                    isEditable: i % 2 === 1
                  }];
                })
              }
            }, overrides);
          };
          return _context.abrupt("return", new Promise(function (resolve, reject) {
            var mockResponse = getMock();
            resolve(mockResponse);
            numberOfLoads += 1;
          }));
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), {
    delay: 10,
    overwriteRoutes: true
  });
};
export var mockActionsExecution = function mockActionsExecution(mockExecutionDelay) {
  fetchMock.post(ORS_ACTIONS_EXECUTION_ENDPOINT, /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(url, request) {
      var body;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            if (!(mockExecutionDelay === Infinity)) {
              _context2.next = 2;
              break;
            }
            return _context2.abrupt("return", new Promise(function () {}));
          case 2:
            body = JSON.parse(request.body);
            if (!(body.integrationKey === 'jira' && body.actionKey === 'atlassian:work-item:get:assignees')) {
              _context2.next = 5;
              break;
            }
            return _context2.abrupt("return", {
              operationStatus: ActionOperationStatus.SUCCESS,
              errors: [],
              entities: cannedUsers.filter(function (user) {
                return user.displayName.toLowerCase().includes((body.parameters.inputs.query || '').toLowerCase());
              })
            });
          case 5:
            if (!(body.integrationKey === 'jira' && body.actionKey === 'atlassian:work-item:get:statuses')) {
              _context2.next = 7;
              break;
            }
            return _context2.abrupt("return", {
              operationStatus: ActionOperationStatus.SUCCESS,
              errors: [],
              entities: cannedStatuses
            });
          case 7:
            if (!((body === null || body === void 0 ? void 0 : body.actionKey) === 'atlassian:work-item:get:priorities')) {
              _context2.next = 9;
              break;
            }
            return _context2.abrupt("return", {
              operationStatus: ActionOperationStatus.SUCCESS,
              errors: [],
              entities: cannedPriorities
            });
          case 9:
            return _context2.abrupt("return", {
              operationStatus: ActionOperationStatus.SUCCESS,
              errors: []
            });
          case 10:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return function (_x, _x2) {
      return _ref2.apply(this, arguments);
    };
  }(), {
    overwriteRoutes: true,
    delay: mockExecutionDelay
  });
};
export var cannedStatuses = [{
  id: '11',
  transitionId: '101',
  text: 'Backlog',
  style: {
    appearance: 'default'
  }
}, {
  id: '21',
  transitionId: '201',
  text: 'Selected for Development',
  style: {
    appearance: 'moved'
  }
}, {
  id: '31',
  transitionId: '301',
  text: 'In Progress',
  style: {
    appearance: 'inprogress'
  }
}, {
  id: '41',
  transitionId: '401',
  text: 'Done',
  style: {
    appearance: 'success'
  }
}, {
  id: '51',
  transitionId: '501',
  text: 'Pending',
  style: {
    appearance: 'removed'
  }
}, {
  id: '61',
  transitionId: '601',
  text: 'some new status',
  style: {
    appearance: 'new'
  }
}];
export var cannedUsers = [{
  atlassianUserId: '5b45501cfc9d8158972cdd2c',
  displayName: 'Mike Dao',
  avatarSource: mike,
  url: 'https://product-fabric.atlassian.net/jira/people/5b45501cfc9d8158972cdd2c'
}, {
  atlassianUserId: '70121:3238cd5c-e4d3-43b1-a125-211cc2811c25',
  displayName: 'Nidhin Joseph',
  avatarSource: nidhin,
  url: 'https://product-fabric.atlassian.net/jira/people/70121:3238cd5c-e4d3-43b1-a125-211cc2811c25'
}, {
  atlassianUserId: '712020:b0ba73bb-ef99-4329-b35d-23e35fffdf5b',
  displayName: 'Aleksandr Sasha Motsjonov',
  avatarSource: sasha,
  url: 'https://product-fabric.atlassian.net/jira/people/712020:b0ba73bb-ef99-4329-b35d-23e35fffdf5b'
}, {
  atlassianUserId: '712020:12f32ca4-7684-4e6d-b3a7-078ae26aa5c8',
  displayName: 'Scott Farquhar',
  avatarSource: profile,
  url: 'https://product-fabric.atlassian.net/jira/people/712020:12f32ca4-7684-4e6d-b3a7-078ae26aa5c8'
}];
var cannedPriorities = [{
  source: blocker,
  label: 'Blocker',
  text: 'Blocker',
  id: '6'
}, {
  source: major,
  label: 'Major',
  text: 'Major',
  id: '5'
}, {
  source: high,
  label: 'High',
  text: 'High',
  id: '4'
}, {
  source: medium,
  text: 'High',
  label: 'Medium',
  id: '3'
}, {
  source: low,
  label: 'Low',
  text: 'Low',
  id: '2'
}, {
  source: trivial,
  label: 'Trivial',
  text: 'Trivial',
  id: '1'
}];