"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _bs = require("react-icons/bs");

var _ri = require("react-icons/ri");

var _lodash = require("lodash");

var _reactRouterDom = require("react-router-dom");

var _configs = require("../../api/services/configs");

var _Dropdown = require("../../components/Dropdown");

var _columns = require("../../components/ConfigViewer/columns");

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

var _components = require("../../components");

var _BreadcrumbNav = require("../../components/BreadcrumbNav");

var _TextInputClearable = require("../../components/TextInputClearable");

var _jsxRuntime = require("react/jsx-runtime");

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; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ConfigListPage() {
  var _useSearchParams = (0, _reactRouterDom.useSearchParams)(),
      _useSearchParams2 = _slicedToArray(_useSearchParams, 2),
      params = _useSearchParams2[0],
      setParams = _useSearchParams2[1];

  var navigate = (0, _reactRouterDom.useNavigate)();

  var _useState = (0, _react.useState)([]),
      _useState2 = _slicedToArray(_useState, 2),
      data = _useState2[0],
      setData = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      _useState4 = _slicedToArray(_useState3, 2),
      filteredData = _useState4[0],
      setFilteredData = _useState4[1];

  var _useState5 = (0, _react.useState)(true),
      _useState6 = _slicedToArray(_useState5, 2),
      isLoading = _useState6[0],
      setIsLoading = _useState6[1];

  var _useOutletContext = (0, _reactRouterDom.useOutletContext)(),
      setTitle = _useOutletContext.setTitle,
      setTitleExtras = _useOutletContext.setTitleExtras;

  var columns = _react.default.useMemo(function () {
    return _columns.defaultTableColumns;
  }, []);

  var query = params.get("query");
  (0, _react.useState)(function () {
    (0, _configs.getAllConfigs)().then(function (res) {
      setData(res.data);
    }).finally(function () {
      setIsLoading(false);
    });
  }, []);

  var handleRowClick = function handleRowClick(row) {
    var _row$original;

    var id = row === null || row === void 0 ? void 0 : (_row$original = row.original) === null || _row$original === void 0 ? void 0 : _row$original.id;

    if (id) {
      navigate("/config/".concat(id));
    }
  };

  var extra = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "flex space-x-2 mr-4",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(TypeDropdown, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(TagsDropdown, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_TextInputClearable.TextInputClearable, {
      onChange: (0, _lodash.debounce)(function (e) {
        var query = e.target.value || "";
        setParams({
          query: query
        });
      }, 200),
      className: "w-80",
      placeholder: "Search for configs",
      defaultValue: params.get("query")
    })]
  });
  (0, _react.useEffect)(function () {
    setTitleExtras(extra);
    return function () {
      return setTitleExtras(null);
    };
  }, []);
  (0, _react.useEffect)(function () {
    var filteredData = data;
    setTitle( /*#__PURE__*/(0, _jsxRuntime.jsx)(_BreadcrumbNav.BreadcrumbNav, {
      list: ["Config"]
    }));

    if ((data === null || data === void 0 ? void 0 : data.length) > 0) {
      // do filtering here
      filteredData = (0, _utils.filterConfigsByText)(filteredData, query);
    }

    setFilteredData(filteredData);
  }, [data, query]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.DataTable, {
      columns: columns,
      data: filteredData,
      handleRowClick: handleRowClick,
      tableStyle: {
        borderSpacing: "0"
      },
      isLoading: isLoading
    })
  });
}

var TypeDropdown = function TypeDropdown(_ref) {
  var rest = _extends({}, _ref);

  var exampleItems = {
    type1: {
      id: "dropdown-type1",
      name: "type1",
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_bs.BsTable, {}),
      description: "Type 1",
      value: "type1"
    },
    type2: {
      id: "dropdown-type2",
      name: "type2",
      icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ri.RiLayoutGridLine, {}),
      description: "Type 2",
      value: "type2"
    }
  };

  var _useState7 = (0, _react.useState)(Object.values(exampleItems)[0].value),
      _useState8 = _slicedToArray(_useState7, 2),
      selected = _useState8[0],
      setSelected = _useState8[1];

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.Dropdown, _objectSpread({
    items: exampleItems,
    onChange: function onChange(value) {
      return setSelected(value);
    },
    value: selected,
    prefix: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "text-xs text-gray-500 mr-2 whitespace-nowrap",
      children: "Type:"
    })
  }, rest));
};

var TagsDropdown = function TagsDropdown(_ref2) {
  var rest = _extends({}, _ref2);

  var exampleItems = {
    tag1: {
      id: "dropdown-tag1",
      name: "tag1",
      description: "Tag 1",
      value: "tag1"
    },
    tag2: {
      id: "dropdown-tag2",
      name: "tag2",
      description: "Tag 2",
      value: "tag2"
    }
  };

  var _useState9 = (0, _react.useState)(Object.values(exampleItems)[0].value),
      _useState10 = _slicedToArray(_useState9, 2),
      selected = _useState10[0],
      setSelected = _useState10[1];

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.Dropdown, _objectSpread({
    items: exampleItems,
    onChange: function onChange(value) {
      return setSelected(value);
    },
    value: selected,
    prefix: /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "text-xs text-gray-500 mr-2 whitespace-nowrap",
        children: "Tag:"
      })
    })
  }, rest));
};