/**
 * amis v3.1.0
 * Copyright 2018-2023 baidu
 */

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function renderCommonStatic(props, defaultValue) {
    var type = props.type, render = props.render, staticSchema = props.staticSchema;
    var staticProps = tslib.__assign(tslib.__assign({}, props), staticSchema);
    switch (type) {
        case 'select':
        case 'checkboxes':
        case 'button-group-select':
        case 'input-tree':
        case 'tree-select':
        case 'nested-select':
        case 'cascader-select':
        case 'radios':
        case 'multi-select':
        case 'transfer':
        case 'transfer-picker':
        case 'tabs-transfer':
        case 'tabs-transfer-picker':
            return render('static-select', { type: 'words' }, staticProps);
        case 'input-date':
        case 'input-datetime':
        case 'input-time':
        case 'input-month':
        case 'input-quarter':
        case 'input-year':
            return renderStaticDateTypes(staticProps);
        case 'input-date-range':
        case 'input-datetime-range':
        case 'input-time-range':
        case 'input-month-range':
        case 'input-quarter-range':
        case 'input-year-range':
            return render('static-input-date-range', { type: 'date-range' }, tslib.__assign(tslib.__assign(tslib.__assign({}, props), { valueFormat: props.format, format: props.inputFormat }), staticSchema));
        case 'input-password':
            return render('static-input-password', { type: 'password' }, staticProps);
        case 'input-color':
            return render('static-color', { type: 'color' }, staticProps);
        case 'input-tag':
            return render('static-input-tag', { type: 'tags' }, staticProps);
        case 'input-url':
            return render('static-input-url', { type: 'link', href: defaultValue }, staticProps);
        case 'input-number':
            return render('static-input-number', { type: 'number' }, tslib.__assign(tslib.__assign({}, props), staticSchema));
        default:
            return defaultValue;
    }
}
/**
 * 表单项类成员render支持静态展示装饰器
 */
function supportStatic() {
    return function (target, name, descriptor) {
        var original = descriptor.value;
        descriptor.value = function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            var props = this.props;
            if (props.static) {
                var render = props.render, staticSchema = props.staticSchema, ns = props.classPrefix, cx = props.classnames, className = props.className, placeholder = props.placeholder, _a = props.staticPlaceholder, staticPlaceholder = _a === void 0 ? (React__default["default"].createElement("span", { className: "text-muted" }, placeholder || '-')) : _a;
                var body = void 0;
                var displayValue = amisCore.getPropValue(props);
                var isValueEmpty = displayValue == null || displayValue === '';
                if (staticSchema &&
                    (staticSchema.type ||
                        Array.isArray(staticSchema) ||
                        typeof staticSchema === 'string' ||
                        typeof staticSchema === 'number')) {
                    // 有自定义schema 且schema有type 时，展示schema
                    body = render('form-static-schema', staticSchema, props);
                }
                else if (target.renderStatic) {
                    // 特殊组件，control有 renderStatic 时，特殊处理
                    body = target.renderStatic.apply(this, tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(args), false), [
                        isValueEmpty ? staticPlaceholder : displayValue
                    ], false));
                }
                else if (isValueEmpty) {
                    // 空值时，展示 staticPlaceholder
                    body = staticPlaceholder;
                }
                else {
                    // 可复用组件 统一处理
                    body = renderCommonStatic(props, displayValue);
                }
                return React__default["default"].createElement("div", { className: cx("".concat(ns, "Form-static"), className) }, body);
            }
            return original.apply(this, args);
        };
        return descriptor;
    };
}
function renderStaticDateTypes(props) {
    var render = props.render, type = props.type, inputFormat = props.inputFormat, timeFormat = props.timeFormat, format = props.format, value = props.value;
    return render('static-input-date', {
        type: 'date',
        value: value,
        format: type === 'time' && timeFormat ? timeFormat : inputFormat,
        valueFormat: format
    });
}

exports.supportStatic = supportStatic;
