import React, { useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { CollapserCard, getGenerators } from '@looker/run-it';
import { DocCode } from '../DocCode';
import { selectSdkLanguage } from '../../state';
import { isMethod } from '../../utils/path';
import { noComment } from './utils';
import { DocDeclarations } from './DocDeclarations';

var getDeclarations = (generators, sdkLanguage, item) => {
  var declarations = {};
  Object.entries(generators).forEach(_ref => {
    var [language, gen] = _ref;

    if (sdkLanguage === 'All' || language === sdkLanguage) {
      var code = isMethod(item) ? gen.declareMethod('', item) : gen.declareType('', item);
      declarations[language] = code;
    }
  });
  return declarations;
};

export var DocSDKs = _ref2 => {
  var {
    api,
    method,
    type
  } = _ref2;
  var sdkLanguage = useSelector(selectSdkLanguage);
  var generators = getGenerators(api);
  var [item, setItem] = useState(method ? noComment(method) : type);
  var [declarations, setDeclarations] = useState(getDeclarations(generators, sdkLanguage, item));
  var [header, setHeader] = useState("".concat(sdkLanguage, " Declaration"));
  useEffect(() => {
    setItem(method ? noComment(method) : type);
  }, [method, type]);
  useEffect(() => {
    var declarations = getDeclarations(generators, sdkLanguage, item);
    setDeclarations(declarations);
    var languages = Object.keys(declarations);

    if (languages.length > 1) {
      setHeader('Declarations');
    } else {
      setHeader("".concat(languages[0], " Declaration"));
    }
  }, [sdkLanguage, item]);
  return React.createElement(CollapserCard, {
    heading: header,
    id: "sdk declarations"
  }, Object.keys(declarations).length > 1 ? React.createElement(DocDeclarations, {
    declarations: declarations
  }) : React.createElement(DocCode, {
    language: Object.keys(declarations)[0],
    code: Object.values(declarations)[0]
  }));
};
//# sourceMappingURL=DocSDKs.js.map