var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import { IconButton } from "../button";
import "./field-item.css";
import textSvg from "../icons/text/text.svg";
import numberSvg from "../icons/num/num.svg";
import timeSvg from "../icons/time/time.svg";
import mediaSvg from "../icons/folder-2/folder-2.svg";
import { Input } from "../input";
var prefixCls = 'dmc-field-item';
var FieldItem = /*#__PURE__*/React.memo(function (props) {
  var _a;

  var type = props.type,
      checked = props.checked,
      name = props.name,
      onClose = props.onClose,
      provided = props.provided,
      dataValue = props.dataValue,
      editable = props.editable,
      changeAlias = props.changeAlias,
      restProps = __rest(props, ["type", "checked", "name", "onClose", "provided", "dataValue", "editable", "changeAlias"]);

  var _b = React.useState(false),
      isEdit = _b[0],
      setIsEdit = _b[1];

  var typeIcon = function typeIcon() {
    var svgSource = '';

    if (type === 'text') {
      svgSource = textSvg;
    } else if (type === 'number') {
      svgSource = numberSvg;
    } else if (type === 'datetime') {
      svgSource = timeSvg;
    } else if (type === 'mediajson') {
      svgSource = mediaSvg;
    }

    if (svgSource) {
      return _jsx("div", {
        style: {
          WebkitMaskImage: "url(" + svgSource + ")",
          WebkitMaskPosition: 'center center',
          WebkitMaskRepeat: 'no-repeat',
          WebkitMaskSize: 'cover'
        },
        className: prefixCls + "-icon"
      }, void 0);
    }

    return null;
  };

  var _onChange = function _onChange(e) {
    setIsEdit(false);
    var value = e.target.value;
    changeAlias && changeAlias(value);
  };

  return _jsxs("div", __assign({}, restProps, {
    className: classNames(prefixCls, (_a = {}, _a[prefixCls + "-checked"] = checked, _a[prefixCls + "-" + type] = type, _a))
  }, provided === null || provided === void 0 ? void 0 : provided.dragHandleProps, {
    children: [typeIcon(), isEdit ? _jsx(Input, {
      className: prefixCls + "-name",
      defaultValue: name,
      allowClear: false,
      autoFocus: true,
      type: "menu-bg",
      onBlur: _onChange,
      onPressEnter: _onChange
    }, void 0) : _jsx("div", __assign({
      className: prefixCls + "-name"
    }, {
      children: name
    }), void 0), editable && _jsx(IconButton, {
      icon: "edit",
      type: "only-icon",
      onClick: function onClick() {
        setIsEdit(true);
      },
      "data-value": dataValue
    }, void 0), onClose && _jsx(IconButton, {
      className: prefixCls + "-close",
      icon: "close",
      type: "only-icon",
      onClick: onClose,
      "data-value": dataValue
    }, void 0)]
  }), void 0);
});
FieldItem.defaultProps = {
  editable: false
};
export default FieldItem;

