var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import { jsx, jsxs } from "react/jsx-runtime";
import React, { useState, useRef, useEffect } from "react";
import styled from "styled-components";
import { string, bool, number, func, shape } from "prop-types";
import FieldSet from "../../Input/web/Fieldset";
import Field from "../../Input/web/Field";
import Label from "../../Input/web/Label";
import Legend from "../../Input/web/Legend";
import Helper from "../../Input/web/Helper";
const Root = styled.div`
  display: inline-block;

  cursor: text;

  ${({ full }) => `
    width: ${full ? "100%" : "auto"};
  `}
`;
const StyledFieldSet = styled(FieldSet)`
  height: 88px;
  box-sizing: border-box;
`;
const StyledField = styled(Field)`
  resize: none;
  height: 100%;
`;
const noop = () => {
};
const TextArea = React.forwardRef(
  (_a, ref) => {
    var _b = _a, {
      disabled = false,
      error,
      full = false,
      helper,
      label = "",
      maxLength,
      className,
      style,
      value = "",
      onChange = noop,
      readOnly = false
    } = _b, props = __objRest(_b, [
      "disabled",
      "error",
      "full",
      "helper",
      "label",
      "maxLength",
      "className",
      "style",
      "value",
      "onChange",
      "readOnly"
    ]);
    const [textAreaValue, setTextAreaValue] = useState(value);
    const [typed, setTyped] = useState(Boolean(value));
    const textAreaRef = ref || useRef(null);
    useEffect(() => {
      setTextAreaValue(value);
      setTyped(Boolean(value));
    }, [value]);
    return /* @__PURE__ */ jsxs(
      Root,
      {
        className,
        full,
        style,
        onClick: () => textAreaRef.current.focus(),
        children: [
          /* @__PURE__ */ jsxs(
            StyledFieldSet,
            {
              error,
              disabled,
              full,
              label,
              children: [
                /* @__PURE__ */ jsx(
                  StyledField,
                  __spreadProps(__spreadValues({}, props), {
                    readOnly,
                    disabled,
                    full,
                    label,
                    as: "textarea",
                    ref: textAreaRef,
                    cleanable: false,
                    maxLength,
                    typed,
                    error,
                    value: textAreaValue,
                    onChange: (e) => {
                      setTextAreaValue(e.target.value);
                      setTyped(Boolean(e.target.value));
                      onChange(e);
                    }
                  })
                ),
                /* @__PURE__ */ jsx(Label, __spreadProps(__spreadValues({ error, disabled }, props), { children: label })),
                label && /* @__PURE__ */ jsx(Legend, { children: label })
              ]
            }
          ),
          /* @__PURE__ */ jsx(
            Helper,
            {
              disabled,
              error,
              helper,
              maxLength,
              length: textAreaValue.length
            }
          )
        ]
      }
    );
  }
);
TextArea.propTypes = {
  className: string,
  disabled: bool,
  /** A error text to be displayed below field */
  error: string,
  full: bool,
  /** A helper text to be displayed below field */
  helper: string,
  label: string,
  /** maximum length (number of characters) of value */
  maxLength: number,
  placeholder: string,
  readOnly: bool,
  style: shape({}),
  value: string,
  onChange: func
};
var TextArea_default = TextArea;
export {
  TextArea_default as default
};
