"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DocSdkUsage = void 0;

var _react = _interopRequireWildcard(require("react"));

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

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

var _runIt = require("@looker/run-it");

var _InsertDriveFile = require("@styled-icons/material-outlined/InsertDriveFile");

var _reactRedux = require("react-redux");

var _state = require("../../state");

var _utils = require("./utils");

var _DocSdkExampleCell = require("./DocSdkExampleCell");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var DocSdkUsage = _ref => {
  var {
    method
  } = _ref;
  var examples = (0, _reactRedux.useSelector)(_state.selectExamplesLode);
  var sdkLanguage = (0, _reactRedux.useSelector)(_state.selectSdkLanguage);
  var [page, setPage] = (0, _react.useState)(1);
  (0, _react.useEffect)(() => {
    setPage(1);
  }, [method]);
  if (!examples) return _react.default.createElement(_react.default.Fragment, null);
  var languages = (0, _sdkCodegen.findExampleLanguages)(examples, method.name);
  if (languages.length === 0) return _react.default.createElement(_react.default.Fragment, null);
  languages = (0, _utils.sortLanguagesByPreference)(languages, sdkLanguage);
  var {
    pageExamples,
    pageLimit,
    paginate
  } = (0, _utils.prepareExampleDataTable)(languages, examples, method.operationId, page);
  if (pageExamples.length === 0) return _react.default.createElement(_react.default.Fragment, null);
  return _react.default.createElement(_runIt.CollapserCard, {
    id: "examples",
    heading: "SDK Examples"
  }, _react.default.createElement(_components.Card, {
    height: "auto",
    px: "small",
    py: "xsmall",
    mt: "small"
  }, _react.default.createElement(_components.DataTable, {
    caption: "SDK Examples",
    columns: _utils.exampleColumns
  }, pageExamples.map((exampleRow, i) => {
    var _pageExamples;

    var isEmptyItem = exampleRow.filename === _utils.EMPTY_STRING;
    var isLastItem = (i + 1) % _utils.PER_PAGE_COUNT === 0 || i + 1 === pageExamples.length;
    var nextItemEmpty = ((_pageExamples = pageExamples[i + 1]) === null || _pageExamples === void 0 ? void 0 : _pageExamples.filename) === _utils.EMPTY_STRING;
    var hidden = isEmptyItem || isLastItem || nextItemEmpty;
    return _react.default.createElement(_components.DataTableItem, {
      id: exampleRow.permalink,
      key: exampleRow.permalink
    }, _react.default.createElement(_DocSdkExampleCell.DocSdkExampleCell, {
      hideBorderBottom: hidden
    }, _react.default.createElement(_components.Space, {
      gap: "xsmall"
    }, !isEmptyItem && _react.default.createElement(_components.Icon, {
      icon: _react.default.createElement(_InsertDriveFile.InsertDriveFile, null),
      color: "text1",
      size: "small"
    }), _react.default.createElement(_components.Link, {
      href: exampleRow.permalink,
      target: '_blank',
      role: "link"
    }, exampleRow.filename))), _react.default.createElement(_DocSdkExampleCell.DocSdkExampleCell, {
      hideBorderBottom: hidden
    }, exampleRow.language), _react.default.createElement(_DocSdkExampleCell.DocSdkExampleCell, {
      hideBorderBottom: hidden
    }, exampleRow.description), _react.default.createElement(_DocSdkExampleCell.DocSdkExampleCell, {
      hideBorderBottom: hidden
    }, exampleRow.line));
  })), paginate && _react.default.createElement(_components.Box, {
    alignSelf: "center"
  }, _react.default.createElement(_components.Pagination, {
    current: page,
    pages: pageLimit,
    onChange: nextPage => {
      setPage(nextPage);
    }
  }))));
};

exports.DocSdkUsage = DocSdkUsage;
//# sourceMappingURL=DocSdkUsage.js.map