"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "defaultInitialVisibleColumnKeys", {
  enumerable: true,
  get: function get() {
    return _data.defaultInitialVisibleColumnKeys;
  }
});
exports.generateResolveResponse = exports.generateDetailsResponse = exports.generateDataResponse = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _data = require("./data");
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) { (0, _defineProperty2.default)(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; }
var defaultDetailsResponse = {
  meta: {
    destinationObjectTypes: ['page', 'attachment', 'blogpost', 'space', 'comment', 'whiteboard', 'database'],
    auth: [],
    product: 'confluence',
    extensionKey: 'confluence-object-provider',
    visibility: 'restricted',
    access: 'granted'
  },
  data: {
    ari: 'ari:cloud:linking-platform::datasource/768fc736-3af4-4a8f-b27e-203602bff8ca',
    id: '768fc736-3af4-4a8f-b27e-203602bff8ca',
    name: 'Confluence Search',
    description: 'For extracting a list of Confluence search Objects',
    parameters: [{
      key: 'searchString',
      type: 'string',
      description: 'Query to search confluence objects'
    }, {
      key: 'cloudId',
      type: 'string',
      description: 'CloudId of the site to search in'
    }, {
      key: 'entityTypes',
      type: 'string',
      description: 'List of entitiy types to search for',
      isList: true
    }, {
      key: 'contentARIs',
      type: 'string',
      description: 'List of ARI of Confluence Object',
      isList: true
    }, {
      key: 'spaceKeys',
      type: 'string',
      description: 'Space keys from which the results are desired',
      isList: true
    }, {
      key: 'contributorAccountIds',
      type: 'string',
      description: 'AccountIds of the users',
      isList: true
    }, {
      key: 'labels',
      type: 'string',
      description: 'Labels which must be present on the result object',
      isList: true
    }, {
      key: 'ancestorPageIds',
      type: 'string',
      description: 'Ids of the pages which must be parent of the result',
      isList: true
    }, {
      key: 'containerStatus',
      type: 'string',
      description: 'Confluence object status'
    }, {
      key: 'contentStatuses',
      type: 'string',
      description: 'Confluence document status',
      isList: true
    }, {
      key: 'creatorAccountIds',
      type: 'string',
      description: 'AccountIds of the users',
      isList: true
    }, {
      key: 'lastModified',
      type: 'string',
      description: 'Represents a point in time, one of: today, yesterday, past7Days, past30Days, pastYear, custom'
    }, {
      key: 'lastModifiedFrom',
      type: 'string',
      description: 'Date that a custom lastModified should be greater than'
    }, {
      key: 'lastModifiedTo',
      type: 'string',
      description: 'Date that a custom lastModified should be less than'
    }, {
      key: 'shouldMatchTitleOnly',
      type: 'boolean',
      description: 'Search for only entities that have a title that contains the given query'
    }],
    schema: {
      defaultProperties: ['type', 'title', 'description', 'space', 'status', 'updatedBy'],
      properties: [{
        key: 'type',
        title: 'Type',
        type: 'icon'
      }, {
        key: 'title',
        title: 'Title',
        type: 'link'
      }, {
        key: 'description',
        title: 'Description',
        type: 'string'
      }, {
        key: 'space',
        title: 'Space',
        type: 'link'
      }, {
        key: 'status',
        title: 'Status',
        type: 'status'
      }, {
        key: 'updatedBy',
        title: 'Updated by',
        type: 'user'
      }, {
        key: 'updatedAt',
        title: 'Updated at',
        type: 'datetime'
      }, {
        key: 'createdBy',
        title: 'Created by',
        type: 'user'
      }, {
        key: 'ownedBy',
        title: 'Owned by',
        type: 'user'
      }, {
        key: 'versionNumber',
        title: 'Version number',
        type: 'number'
      }, {
        key: 'createdAt',
        title: 'Created at',
        type: 'datetime'
      }, {
        key: 'labels',
        title: 'Labels',
        type: 'tag',
        isList: true
      }]
    }
  }
};
var generateDetailsResponse = exports.generateDetailsResponse = function generateDetailsResponse(initialColumnKeys) {
  return _objectSpread(_objectSpread({}, defaultDetailsResponse), {}, {
    meta: _objectSpread(_objectSpread({}, defaultDetailsResponse.meta), {}, {
      schema: _objectSpread(_objectSpread({}, defaultDetailsResponse.meta.schema), {}, {
        defaultProperties: initialColumnKeys
      })
    })
  });
};
var buildDataResponse = function buildDataResponse(_ref) {
  var initialVisibleColumnKeys = _ref.initialVisibleColumnKeys,
    _ref$numberOfLoads = _ref.numberOfLoads,
    numberOfLoads = _ref$numberOfLoads === void 0 ? 0 : _ref$numberOfLoads,
    includeSchema = _ref.includeSchema,
    _ref$isUnauthorized = _ref.isUnauthorized,
    isUnauthorized = _ref$isUnauthorized === void 0 ? false : _ref$isUnauthorized,
    _ref$includeAuthInfo = _ref.includeAuthInfo,
    includeAuthInfo = _ref$includeAuthInfo === void 0 ? false : _ref$includeAuthInfo,
    _ref$maxItems = _ref.maxItems,
    maxItems = _ref$maxItems === void 0 ? 99 : _ref$maxItems;
  var schema = {
    properties: defaultDetailsResponse.data.schema.properties.filter(function (_ref2) {
      var key = _ref2.key;
      return initialVisibleColumnKeys.includes(key);
    })
  };
  return {
    meta: {
      access: isUnauthorized ? 'unauthorized' : 'granted',
      // TODO: further refactoring in EDM-9573
      // https://stash.atlassian.com/projects/ATLASSIAN/repos/atlassian-frontend-monorepo/pull-requests/82725/overview?commentId=6798084
      auth: includeAuthInfo ? [{
        key: 'confluence',
        displayName: 'Atlassian Links - Confluence',
        url: 'https://id.atlassian.com/login'
      }] : [],
      definitionId: 'object-resolver-service',
      destinationObjectTypes: ['page', 'attachment', 'blogpost', 'space', 'comment', 'whiteboard', 'database'],
      key: 'confluence-object-provider',
      product: 'confluence',
      visibility: 'restricted'
    },
    data: _objectSpread({
      items: _data.mockConfluenceData.data.slice(0, maxItems).map(function (item, idx) {
        return _objectSpread(_objectSpread({}, item), {}, {
          // Fake identifier attribute that is a primitive value.
          // Adding number of pages to make all issueNumbers unique
          id: {
            data: "".concat(item.id.data) + numberOfLoads
          }
        });
      }),
      totalCount: maxItems,
      nextPageCursor: numberOfLoads < 4 && maxItems > 1 ? 'c3RhcnRBdD01' : undefined
    }, includeSchema && {
      schema: schema
    })
  };
};
var generateDataResponse = exports.generateDataResponse = function generateDataResponse(_ref3) {
  var _ref3$cloudId = _ref3.cloudId,
    cloudId = _ref3$cloudId === void 0 ? '' : _ref3$cloudId,
    _ref3$numberOfLoads = _ref3.numberOfLoads,
    numberOfLoads = _ref3$numberOfLoads === void 0 ? 0 : _ref3$numberOfLoads,
    _ref3$includeSchema = _ref3.includeSchema,
    includeSchema = _ref3$includeSchema === void 0 ? true : _ref3$includeSchema,
    _ref3$initialVisibleC = _ref3.initialVisibleColumnKeys,
    initialVisibleColumnKeys = _ref3$initialVisibleC === void 0 ? _data.defaultInitialVisibleColumnKeys : _ref3$initialVisibleC;
  switch (cloudId) {
    case '22222':
    case 'mock-no-results':
      return buildDataResponse({
        cloudId: cloudId,
        numberOfLoads: numberOfLoads,
        includeSchema: includeSchema,
        initialVisibleColumnKeys: initialVisibleColumnKeys,
        maxItems: 0
      });
    case '44444': // to move away to more readable cloud ids, but the test helpers are still relying on numeric cloudIds
    case 'mock-unauth':
      return buildDataResponse({
        cloudId: cloudId,
        numberOfLoads: numberOfLoads,
        includeSchema: includeSchema,
        initialVisibleColumnKeys: initialVisibleColumnKeys,
        isUnauthorized: true
      });
    case '11111':
    case 'mock-1-item':
      return buildDataResponse({
        cloudId: cloudId,
        maxItems: 1,
        numberOfLoads: numberOfLoads,
        includeSchema: includeSchema,
        initialVisibleColumnKeys: initialVisibleColumnKeys
      });
    case 'mock-unauthorized-unauth':
      return buildDataResponse({
        cloudId: cloudId,
        numberOfLoads: numberOfLoads,
        includeSchema: includeSchema,
        isUnauthorized: true,
        initialVisibleColumnKeys: initialVisibleColumnKeys,
        includeAuthInfo: true
      });
    case '33333':
    case 'mock-error':
      // to move away to more readable cloud ids, but the test helpers are still relying on numeric cloudIds
      throw new Error('Mock error');
    default:
      return buildDataResponse({
        cloudId: cloudId,
        numberOfLoads: numberOfLoads,
        includeSchema: includeSchema,
        initialVisibleColumnKeys: initialVisibleColumnKeys
      });
  }
};
var resolveConfluenceSearch = {
  body: {
    meta: {
      auth: [],
      definitionId: 'confluence-object-provider',
      product: 'confluence',
      visibility: 'granted',
      access: 'granted',
      resourceType: 'search',
      objectId: 'text=searchsomething',
      tenantId: 'DUMMY-a5a01d21-1cc3-4f29-9565-f2bb8cd969f5',
      category: 'object',
      key: 'confluence-object-provider'
    },
    data: {
      '@context': {
        '@vocab': 'https://www.w3.org/ns/activitystreams#',
        atlassian: 'https://schema.atlassian.com/ns/vocabulary#',
        schema: 'http://schema.org/'
      },
      generator: {
        '@type': 'Application',
        '@id': 'https://www.atlassian.com/#Confluence',
        name: 'Confluence',
        icon: {
          '@type': 'Image',
          url: 'https://cdn.bfldr.com/K3MHR9G8/at/hcwrtqnwvz33bn73b84ncpt5/confluence-mark-contained-gradient-blue.svg?auto=webp&format=png&width=320&height=320'
        }
      },
      '@type': ['Object'],
      url: 'https://pug.jira-dev.com/wiki/search?text=searchsomething',
      name: 'Confluence search with 0 results',
      'atlassian:titlePrefix': {
        text: '',
        '@type': 'atlassian:Emoji'
      }
    },
    datasources: [{
      key: 'datasource-confluence-search',
      parameters: {
        searchString: 'searchsomething'
      },
      id: '768fc736-3af4-4a8f-b27e-203602bff8ca',
      ari: 'ari:cloud:linking-platform::datasource/768fc736-3af4-4a8f-b27e-203602bff8ca',
      description: 'For extracting a list of Confluence search results',
      name: 'Confluence search'
    }]
  },
  status: 200
};
var generateResolveResponse = exports.generateResolveResponse = function generateResolveResponse(resourceUrl) {
  var url = new URL(resourceUrl);
  if (url.search.includes('wiki/')) {
    return resolveConfluenceSearch;
  }
};