import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.filter.js";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.array.push.js";
import "core-js/modules/es.object.define-properties.js";
import "core-js/modules/es.object.define-property.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/esnext.iterator.constructor.js";
import "core-js/modules/esnext.iterator.filter.js";
import "core-js/modules/esnext.iterator.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import classNames from 'classnames';
import React, { useRef, useState } from 'react';
import { FcrIcon } from '../icon';
import './index.css';
import { FcrIconType } from '../icon/type';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var FcrInput = function FcrInput(_ref) {
  var placeholder = _ref.placeholder,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? '' : _ref$value,
    disabled = _ref.disabled,
    iconPrefix = _ref.iconPrefix,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'medium' : _ref$size,
    label = _ref.label,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
    _ref$onBlur = _ref.onBlur,
    onBlur = _ref$onBlur === void 0 ? function () {} : _ref$onBlur,
    _ref$onKeyDown = _ref.onKeyDown,
    onKeyDown = _ref$onKeyDown === void 0 ? function () {} : _ref$onKeyDown,
    _ref$onKeyUp = _ref.onKeyUp,
    onKeyUp = _ref$onKeyUp === void 0 ? function () {} : _ref$onKeyUp,
    _ref$allowClear = _ref.allowClear,
    allowClear = _ref$allowClear === void 0 ? true : _ref$allowClear,
    autoFocus = _ref.autoFocus,
    style = _ref.style,
    _ref$shape = _ref.shape,
    shape = _ref$shape === void 0 ? 'circle' : _ref$shape,
    maxLength = _ref.maxLength,
    showCount = _ref.showCount,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'text' : _ref$type;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    focused = _useState2[0],
    setFocused = _useState2[1];
  var inputRef = useRef(null);
  var handleFocus = function handleFocus() {
    setFocused(true);
  };
  var handleBlur = function handleBlur() {
    setFocused(false);
    onBlur();
  };
  var handleClear = function handleClear() {
    onChange('');
  };
  var handleChange = function handleChange(e) {
    onChange(e.target.value);
  };
  var handleClick = function handleClick() {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  };
  var cls = classNames('fcr-input', "fcr-input-".concat(shape), {
    'fcr-input--focused': focused,
    'fcr-input-l': size === 'large',
    'fcr-input-m': size === 'medium',
    'fcr-input-s': size === 'small',
    'fcr-input-xs': size === 'mini',
    'fcr-input--disabled': disabled
  });
  var iconWrapCls = classNames('fcr-input-icon-wrap', 'fcr-no-drag', {
    'fcr-input-icon-wrap--invisible': disabled || !focused || value.length === 0
  });
  var iconCls = classNames('fcr-input-icon', {});
  var labelCls = classNames('fcr-input-label', {});
  return /*#__PURE__*/_jsxs(React.Fragment, {
    children: [label && /*#__PURE__*/_jsx("label", {
      className: labelCls,
      children: label
    }), /*#__PURE__*/_jsxs("div", {
      className: cls,
      style: style,
      onClick: handleClick,
      children: [iconPrefix && /*#__PURE__*/_jsx(FcrIcon, _objectSpread({
        style: {
          marginRight: 10
        },
        size: 18
      }, iconPrefix)), /*#__PURE__*/_jsx("input", {
        className: classNames('', {
          'not-empty': (value === null || value === void 0 ? void 0 : value.length) > 0 ? true : false
        }),
        maxLength: maxLength,
        ref: inputRef,
        placeholder: placeholder,
        onFocus: handleFocus,
        onBlur: handleBlur,
        value: value,
        disabled: disabled,
        onChange: handleChange,
        onKeyDown: onKeyDown,
        onKeyUp: onKeyUp,
        autoFocus: autoFocus,
        type: type
      }), /*#__PURE__*/_jsxs("div", {
        className: "fcr-input-suffix",
        children: [allowClear && /*#__PURE__*/_jsx("div", {
          className: iconWrapCls,
          onMouseDown: handleClear,
          children: /*#__PURE__*/_jsx(FcrIcon, {
            className: iconCls,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_icontext1)'
            },
            type: FcrIconType.FCR_CLOSE,
            size: 18
          })
        }), showCount && maxLength && /*#__PURE__*/_jsxs("span", {
          className: "fcr-input-count",
          children: [/*#__PURE__*/_jsxs("span", {
            className: classNames('fcr-input-normal', {
              'fcr-input-count-error': value.length === maxLength
            }),
            children: [value.length, ' ']
          }), "/", maxLength]
        })]
      })]
    })]
  });
};