import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { List } from 'antd';
import { Link } from 'react-router-dom';
import PageContext from '../contexts/page';
import { columnType } from './types';
import { useAppContext } from '../contexts/AppContext';
import { jsx as _jsx } from "react/jsx-runtime";
export default function RefTableLinks({
  value,
  column
}) {
  const {
    getTablesByDbName
  } = useAppContext();
  const {
    dbName
  } = useContext(PageContext);
  // val can be "123" or ["123", "456"]
  let ids = [];
  if (Array.isArray(value)) {
    ids = value;
  } else {
    ids = value === null ? [] : [value];
  }
  const foundRefTable = getTablesByDbName(dbName).find(tb => tb.name === column.referenceTable);
  if (!foundRefTable) {
    return /*#__PURE__*/_jsx("div", {
      children: "Ref table not found"
    });
  }
  const foundRefTableColumn = foundRefTable.columns.find(col => col.primary);
  if (!foundRefTableColumn) {
    return /*#__PURE__*/_jsx("div", {
      children: "Ref table primary column not found"
    });
  }
  const refTablePrimaryKey = foundRefTableColumn.id;
  return /*#__PURE__*/_jsx("span", {
    className: "ref-table",
    children: /*#__PURE__*/_jsx(List, {
      size: "small",
      dataSource: ids,
      renderItem: id => /*#__PURE__*/_jsx(List.Item, {
        children: /*#__PURE__*/_jsx(Link, {
          to: `/${dbName}/${column.referenceTable}/get?${refTablePrimaryKey}=${id}`,
          children: id
        })
      })
    })
  });
}
RefTableLinks.propTypes = {
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]).isRequired,
  column: columnType.isRequired
};
//# sourceMappingURL=RefTableLinks.js.map