import _construct from "@babel/runtime/helpers/esm/construct";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useContext, useEffect } from "react";
import useIsLabelOnlyMode from "../../hooks/use-is-label-only-mode";
import { useAppConfig } from "../AppConfig";
import { LabelHelpContext } from "./LabelHelpProvider.js";
import computeDatasetVariable from "../../utils/compute-dataset-variable";
import useDataset from "../../hooks/use-dataset";
export var useLabelHelp = function useLabelHelp() {
  var _dataset$samples;

  var isLabelOnlyMode = useIsLabelOnlyMode();

  var _useDataset = useDataset(),
      _useDataset2 = _slicedToArray(_useDataset, 1),
      dataset = _useDataset2[0];

  var _useContext = useContext(LabelHelpContext),
      pricingConfig = _useContext.pricingConfig,
      myCredits = _useContext.myCredits,
      remoteFile = _useContext.remoteFile,
      loadPricingConfig = _useContext.loadPricingConfig,
      loadMyCredits = _useContext.loadMyCredits;

  var _useAppConfig = useAppConfig(),
      fromConfig = _useAppConfig.fromConfig;

  var labelHelpDisabled = Boolean(fromConfig("labelhelp.disabled"));
  var hasLabelHelpAPIKey = Boolean(fromConfig("labelhelp.apikey"));
  useEffect(function () {
    if (labelHelpDisabled || isLabelOnlyMode) return;
    if (!dataset) return;
    if (!pricingConfig) loadPricingConfig();
  }, [pricingConfig, isLabelOnlyMode, labelHelpDisabled, loadPricingConfig, dataset]);
  if (!dataset) return {
    labelHelpEnabled: false,
    labelHelpError: "no active dataset"
  };
  if (labelHelpDisabled) return {
    labelHelpEnabled: false,
    labelHelpError: "Disabled in config"
  };
  if (isLabelOnlyMode) return {
    labelHelpEnabled: false
  };

  if (!hasLabelHelpAPIKey && (dataset === null || dataset === void 0 ? void 0 : (_dataset$samples = dataset.samples) === null || _dataset$samples === void 0 ? void 0 : _dataset$samples.length) < 100) {
    return {
      labelHelpEnabled: false,
      labelHelpError: "Less than 100 samples"
    };
  }

  if (!pricingConfig) {
    return {
      labelHelpEnabled: false,
      labelHelpError: "No pricing config"
    };
  }

  try {
    var _ref = pricingConfig[dataset.interface.type] || {},
        formula = _ref.formula,
        variableDescriptions = _ref.variables;

    if (!formula) return {
      labelHelpEnabled: false,
      labelHelpError: "No pricing formula for \"".concat(dataset.interface.type, "\"")
    };
    var variables = {};

    for (var _i = 0, _Object$keys = Object.keys(variableDescriptions); _i < _Object$keys.length; _i++) {
      var variableName = _Object$keys[_i];
      variables[variableName] = computeDatasetVariable(dataset, variableName);
    }

    var funcArgs = Object.keys(variables); // TODO sanitize formula
    // eslint-disable-next-line

    var formulaFuncPos = _construct(Function, _toConsumableArray(funcArgs).concat(["return " + formula]));

    var formulaFunc = function formulaFunc(variables) {
      return formulaFuncPos.apply(void 0, _toConsumableArray(funcArgs.map(function (ak) {
        return variables[ak];
      })));
    };

    var totalCost = formulaFunc(variables);
    return {
      labelHelpEnabled: true,
      myCredits: myCredits,
      loadMyCredits: loadMyCredits,
      remoteFile: remoteFile,
      formula: formula,
      variableDescriptions: variableDescriptions,
      variables: variables,
      formulaFunc: formulaFunc,
      totalCost: totalCost,
      labelHelpAPIKey: fromConfig("labelhelp.apikey")
    };
  } catch (e) {
    return {
      labelHelpEnabled: false,
      labelHelpError: e.toString()
    };
  }
};
export default useLabelHelp;