"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var button_1 = require("../button");

require("./field-item.css");

var text_svg_1 = __importDefault(require("../icons/text/text.svg"));

var num_svg_1 = __importDefault(require("../icons/num/num.svg"));

var time_svg_1 = __importDefault(require("../icons/time/time.svg"));

var folder_2_svg_1 = __importDefault(require("../icons/folder-2/folder-2.svg"));

var input_1 = require("../input");

var prefixCls = 'dmc-field-item';
var FieldItem = 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 = text_svg_1["default"];
    } else if (type === 'number') {
      svgSource = num_svg_1["default"];
    } else if (type === 'datetime') {
      svgSource = time_svg_1["default"];
    } else if (type === 'mediajson') {
      svgSource = folder_2_svg_1["default"];
    }

    if (svgSource) {
      return jsx_runtime_1.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 jsx_runtime_1.jsxs("div", __assign({}, restProps, {
    className: classnames_1["default"](prefixCls, (_a = {}, _a[prefixCls + "-checked"] = checked, _a[prefixCls + "-" + type] = type, _a))
  }, provided === null || provided === void 0 ? void 0 : provided.dragHandleProps, {
    children: [typeIcon(), isEdit ? jsx_runtime_1.jsx(input_1.Input, {
      className: prefixCls + "-name",
      defaultValue: name,
      allowClear: false,
      autoFocus: true,
      type: "menu-bg",
      onBlur: _onChange,
      onPressEnter: _onChange
    }, void 0) : jsx_runtime_1.jsx("div", __assign({
      className: prefixCls + "-name"
    }, {
      children: name
    }), void 0), editable && jsx_runtime_1.jsx(button_1.IconButton, {
      icon: "edit",
      type: "only-icon",
      onClick: function onClick() {
        setIsEdit(true);
      },
      "data-value": dataValue
    }, void 0), onClose && jsx_runtime_1.jsx(button_1.IconButton, {
      className: prefixCls + "-close",
      icon: "close",
      type: "only-icon",
      onClick: onClose,
      "data-value": dataValue
    }, void 0)]
  }), void 0);
});
FieldItem.defaultProps = {
  editable: false
};
exports["default"] = FieldItem;

