"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getFileNameFromPath = getFileNameFromPath;
exports.prepareExampleDataTable = prepareExampleDataTable;
exports.getTableItemBottomBorder = getTableItemBottomBorder;
exports.sortLanguagesByPreference = sortLanguagesByPreference;
exports.exampleColumns = exports.EMPTY_STRING = exports.PER_PAGE_COUNT = void 0;

var _sdkCodegen = require("@looker/sdk-codegen");

var _components = require("@looker/components");

var PER_PAGE_COUNT = 10;
exports.PER_PAGE_COUNT = PER_PAGE_COUNT;
var EMPTY_STRING = '';
exports.EMPTY_STRING = EMPTY_STRING;
var exampleColumns = [{
  id: 'filename',
  title: 'Filename',
  type: 'string',
  size: 30
}, {
  id: 'language',
  title: 'Language',
  type: 'string',
  size: 20
}, {
  id: 'description',
  title: 'Description',
  type: 'string',
  size: 40
}, {
  id: 'line',
  title: 'Line',
  type: 'number',
  size: 20
}];
exports.exampleColumns = exampleColumns;

function getFileNameFromPath(path) {
  var lastForwardSlash = path.lastIndexOf('/');
  return lastForwardSlash === -1 ? path : path.slice(lastForwardSlash + 1, path.indexOf(' '));
}

function prepareExampleDataTable(languages, examples, operationId, currentPage) {
  var tableExamples = languages.reduce(function (result, language) {
    var languageExamples = (0, _sdkCodegen.findExamples)(examples, language, operationId);
    return [...result, ...languageExamples.map(exampleLink => {
      return {
        filename: getFileNameFromPath(exampleLink.tooltip),
        line: exampleLink.lineNumber,
        language: language.toLowerCase(),
        permalink: exampleLink.permalink,
        description: exampleLink.description
      };
    })];
  }, []);
  var pageExamples = tableExamples.filter((example, index) => example && index + 1 > (currentPage - 1) * PER_PAGE_COUNT && index + 1 <= currentPage * PER_PAGE_COUNT);
  var pageLimit = Math.ceil(tableExamples.length / PER_PAGE_COUNT);
  var paginate = tableExamples.length > PER_PAGE_COUNT;

  if (tableExamples.length > PER_PAGE_COUNT) {
    while (pageExamples.length !== PER_PAGE_COUNT) {
      pageExamples.push({
        filename: EMPTY_STRING,
        line: EMPTY_STRING,
        language: EMPTY_STRING,
        permalink: pageExamples.length.toString(),
        description: EMPTY_STRING
      });
    }
  }

  return {
    pageExamples,
    pageLimit,
    paginate
  };
}

function getTableItemBottomBorder(hide) {
  return hide ? 'none' : "solid 1px ".concat(_components.theme.colors.ui2);
}

function sortLanguagesByPreference(languages, sdkLanguage) {
  languages.some((item, idx) => item === sdkLanguage && languages.unshift(languages.splice(idx, 1)[0]));
  return languages;
}
//# sourceMappingURL=utils.js.map