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

import React, { useState, useEffect, useRef } from "react";
import PropTypes from "prop-types";
import _ from "lodash";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";
import { IdUnico } from "@intersoft/utiles";
import { Depura } from "@intersoft/depura"; //------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------

var TAG = "CbxStrValueLabel>"; //------------------------------------------------------------------------------------

var dep = new Depura(TAG, false); //------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------

var V = "1.0.0"; //------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// STRING VALUE
// ------------
// recibe un array de objetos, y una propiedad <value> que es el valor a buscar
// y que contiene una propiedad <label> que es el valor a mostrar
// devuelve el objeto encontrado, o null si no lo encuentra
// Ej,:  { value: "a", label: 'The Shawshank Redemption', year: 1994 },
//----------------------------------------------------------------------------------

var CbxStrValueLabel = function CbxStrValueLabel(props) {
  var _useState = useState(props.defaultValue),
      _useState2 = _slicedToArray(_useState, 2),
      valor = _useState2[0],
      setValor = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      hasFocus = _useState4[0],
      setFocus = _useState4[1]; //----------------------------------------------------------------------------------


  var data = props.data,
      label = props.label,
      width = props.width,
      verBtnClear = props.verBtnClear,
      defaultValue = props.defaultValue;
  var ancho = {
    width: width
  };
  var id = IdUnico.getCorto("-cbx-str-val-lbl-");
  dep.logC("id: " + id); //----------------------------------------------------------------------------------

  useEffect(function () {
    dep.logTit("useEffect->hasFocus");
    dep.logG(hasFocus);

    if (_.isFunction(props.onFocus)) {
      props.onFocus(hasFocus);
    }
  }, [hasFocus]); //----------------------------------------------------------------------------------

  return /*#__PURE__*/React.createElement(Autocomplete, {
    disablePortal: true,
    disableClearable: !verBtnClear,
    id: id,
    options: data,
    getOptionLabel: function getOptionLabel(option) {
      dep.log(option);

      if (option === Object(option)) {
        dep.logC("es objeto");
        var c = data.filter(function (obj) {
          return obj.label == option.label;
        });

        if (c.length > 0) {
          dep.log(c[0]);
          return c[0].label;
        } else {
          return "";
        }
      } else {
        dep.logC("NO es objeto, es solo el value...");

        var _c = data.filter(function (obj) {
          return obj.value == option;
        });

        if (_c.length > 0) {
          dep.log(_c[0]);
          dep.log(_c[0].label);
          return _c[0].label;
        } else {
          return "";
        }
      }
    },
    value: valor,
    defaultValue: defaultValue,
    sx: ancho,
    onChange: function onChange(event, newValue) {
      dep.logTit("CbxStrValueLabel");
      dep.obj(newValue);

      if (newValue == null) {
        setValor("");
      } else {
        setValor(newValue.value);
      }

      if (_.isFunction(props.onChange)) {
        props.onChange(newValue);
      }

      dep.log("*********************");
    },
    renderInput: function renderInput(params) {
      return /*#__PURE__*/React.createElement(TextField, _extends({}, params, {
        label: label,
        onFocus: function onFocus() {
          return setFocus(true);
        },
        onBlur: function onBlur() {
          return setFocus(false);
        }
      }));
    }
  });
}; //------------------------------------------------------------------------------------


CbxStrValueLabel.propTypes = {
  label: PropTypes.string,
  data: PropTypes.array,
  width: PropTypes.number,
  verBtnClear: PropTypes.bool,
  defaultValue: PropTypes.string,
  onChange: PropTypes.func,
  onFocus: PropTypes.func
}; //------------------------------------------------------------------------------------

CbxStrValueLabel.defaultProps = {
  label: "",
  data: [],
  width: 300,
  verBtnClear: true,
  defaultValue: ""
}; //------------------------------------------------------------------------------------

export default CbxStrValueLabel;