import _pickBy from "ramda/src/pickBy";
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useRef, useLayoutEffect, useEffect, isValidElement } from 'react';
import styled from 'styled-components';
import { FormField, Flexbox, Typography } from '../../..';
import NormalizedElements from '../../../common/NormalizedElements';
var DEFAULT_WIDTH = 200;
var hasError = function hasError(error) {
  return error && error !== '';
};
function getComponentWidth(fullWidth, width) {
  if (fullWidth) {
    return '100%';
  }
  if (typeof width === 'number') {
    return "".concat(width, "px");
  }
  if (typeof width === 'string' && width !== '') {
    return width;
  }
  return undefined;
}
var AddonBox = styled(Typography).withConfig({
  displayName: "QuietText__AddonBox",
  componentId: "sc-r8jj3e-0"
})(["display:inline-block;color:", ";"], function (p) {
  return p.$disabled ? p.theme.colorTokens.neutral.text_disabled : p.theme.colorTokens.neutral.text_default;
});
var Input = styled(NormalizedElements.Input).attrs(function (p) {
  return {
    type: p.type || 'text'
  };
}).withConfig({
  displayName: "QuietText__Input",
  componentId: "sc-r8jj3e-1"
})(["border:0;padding:0;margin:0;width:", "px;background-color:transparent;box-sizing:border-box;color:", ";font-weight:inherit;line-height:inherit;&::placeholder{line-height:inherit;opacity:1;color:", ";}&:focus{outline:none;}&:disabled{color:", ";}"], function (p) {
  return p.$inputWidth;
}, function (p) {
  return p.theme.colorTokens.neutral.text_default;
}, function (p) {
  return p.theme.colorTokens.neutral.text_disabled;
}, function (p) {
  return p.theme.colorTokens.neutral.text_disabled;
});
var Container = styled(Flexbox).withConfig({
  displayName: "QuietText__Container",
  componentId: "sc-r8jj3e-2"
})(["", " height:", "px;min-width:", "px;display:inline-flex;gap:", "px;align-items:baseline;position:relative;border-bottom:2px solid ", ";&:hover{border-color:", ";}&:focus-within{border-color:", ";}", ""], function (p) {
  return p.$width ? "width: ".concat(p.$width, ";") : '';
}, function (p) {
  return p.theme.spacing.unit(9);
}, DEFAULT_WIDTH, function (p) {
  return p.theme.spacing.unit(2);
}, function (p) {
  if (p.$error) return p.theme.colorTokens.error.border_default;
  if (p.$success) return p.theme.colorTokens.positive.border_default;
  return p.theme.colorTokens.neutral.border_default;
}, function (p) {
  return p.theme.colorTokens.neutral.border_hover;
}, function (p) {
  return p.theme.colorTokens.neutral.border_active;
}, function (p) {
  return p.$disabled && "\n    border-color: ".concat(p.theme.colorTokens.neutral.border_disabled, ";\n    cursor: not - allowed;\n    ");
});
var HiddenMeasuringSpan = styled.span.withConfig({
  displayName: "QuietText__HiddenMeasuringSpan",
  componentId: "sc-r8jj3e-3"
})(["visibility:hidden;position:absolute;white-space:nowrap;height:0;"]);
var components = {
  Container: Container,
  Input: Input,
  AddonBox: AddonBox
};
var getAriaProps = _pickBy(function (val, key) {
  return key.startsWith('aria-');
});
var getDataProps = _pickBy(function (val, key) {
  return key.startsWith('data-');
});
var QuietTextComponent = React.forwardRef(function (props, ref) {
  var autoComplete = props.autoComplete,
    autoFocus = props.autoFocus,
    defaultValue = props.defaultValue,
    fullWidth = props.fullWidth,
    id = props.id,
    inputMode = props.inputMode,
    leftAddon = props.leftAddon,
    maxLength = props.maxLength,
    name = props.name,
    onBlur = props.onBlur,
    onChange = props.onChange,
    onClick = props.onClick,
    onFocus = props.onFocus,
    onKeyDown = props.onKeyDown,
    onKeyPress = props.onKeyPress,
    onKeyUp = props.onKeyUp,
    onMouseEnter = props.onMouseEnter,
    onMouseLeave = props.onMouseLeave,
    pattern = props.pattern,
    placeholder = props.placeholder,
    readOnly = props.readOnly,
    required = props.required,
    rightAddon = props.rightAddon,
    success = props.success,
    type = props.type,
    value = props.value,
    visuallyEmphasiseRequired = props.visuallyEmphasiseRequired,
    className = props.className,
    disabled = props.disabled,
    error = props.error,
    extraInfo = props.extraInfo,
    hideLabel = props.hideLabel,
    label = props.label,
    labelTooltip = props.labelTooltip,
    labelTooltipInModal = props.labelTooltipInModal,
    labelTooltipPosition = props.labelTooltipPosition,
    width = props.width;
  var formFieldProps = {
    className: className,
    disabled: disabled,
    error: error,
    extraInfo: extraInfo,
    fieldId: id,
    hideLabel: hideLabel,
    label: label,
    labelTooltip: labelTooltip,
    labelTooltipInModal: labelTooltipInModal,
    labelTooltipPosition: labelTooltipPosition,
    required: visuallyEmphasiseRequired,
    width: width
  };
  var _useState = useState(value || defaultValue || placeholder || ''),
    _useState2 = _slicedToArray(_useState, 2),
    inputValue = _useState2[0],
    setInputValue = _useState2[1];
  var _useState3 = useState(DEFAULT_WIDTH),
    _useState4 = _slicedToArray(_useState3, 2),
    inputWidth = _useState4[0],
    setInputWidth = _useState4[1];
  var hiddenMeasuringSpanRef = useRef(null);
  var isControlled = value !== undefined;
  var componentWidth = getComponentWidth(fullWidth, width);
  useLayoutEffect(function () {
    var _hiddenMeasuringSpanR;
    var spanWdith = (_hiddenMeasuringSpanR = hiddenMeasuringSpanRef.current) === null || _hiddenMeasuringSpanR === void 0 ? void 0 : _hiddenMeasuringSpanR.offsetWidth;
    setInputWidth(spanWdith || DEFAULT_WIDTH);
  }, [inputValue]);
  useEffect(function () {
    if (isControlled) {
      setInputValue(value);
    }
  }, [value, isControlled]);
  function handleOnInput(e) {
    if (!isControlled) {
      setInputValue(e.target.value || placeholder || '');
    }
  }
  return React.createElement(FormField, formFieldProps, React.createElement(Typography, {
    type: "title1",
    weight: "bold",
    color: "inherit"
  }, React.createElement(Container, {
    $error: !!error,
    $disabled: disabled,
    $success: success,
    $width: componentWidth
  }, isValidElement(leftAddon) ? leftAddon : leftAddon && React.createElement(AddonBox, {
    type: "primary",
    $disabled: disabled
  }, leftAddon), React.createElement(Input, _extends({
    autoComplete: autoComplete,
    autoFocus: autoFocus,
    defaultValue: defaultValue,
    disabled: disabled,
    error: error,
    id: id,
    leftAddon: leftAddon,
    maxLength: maxLength,
    name: name,
    onBlur: onBlur,
    onChange: onChange,
    onClick: onClick,
    onFocus: onFocus,
    onInput: handleOnInput,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onKeyDown: onKeyDown,
    onKeyPress: onKeyPress,
    onKeyUp: onKeyUp,
    placeholder: placeholder,
    required: required,
    rightAddon: rightAddon,
    success: success,
    value: value,
    type: type,
    ref: ref,
    readOnly: readOnly,
    pattern: pattern,
    inputMode: inputMode,
    $inputWidth: inputWidth
  }, getAriaProps(props), getDataProps(props), hasError(error) ? {
    'aria-invalid': true
  } : {})), isValidElement(rightAddon) ? rightAddon : rightAddon && React.createElement(AddonBox, {
    type: "primary",
    $disabled: disabled
  }, rightAddon), React.createElement(HiddenMeasuringSpan, {
    ref: hiddenMeasuringSpanRef,
    "aria-hidden": true
  }, inputValue))));
});
export var QuietText = QuietTextComponent;
QuietText.components = components;