"use strict";
const uni_modules_tdesignUniapp_components_common_validator = require("../validator.js");
const uni_modules_tdesignUniapp_components_common_version = require("../version.js");
const uni_modules_tdesignUniapp_components_common_utils = require("../utils.js");
const getInnerControlledValue = (key) => `data${key.replace(/^(\w)/, (e, t) => t.toUpperCase())}`;
const getDefaultKey = (key) => `default${key.replace(/^(\w)/, (e, t) => t.toUpperCase())}`;
const ARIAL_PROPS = [
  { key: "ariaHidden", type: Boolean },
  { key: "ariaRole", type: String },
  { key: "ariaLabel", type: String },
  { key: "ariaLabelledby", type: String },
  { key: "ariaDescribedby", type: String },
  { key: "ariaBusy", type: Boolean }
];
const getPropsDefault = (type, disableBoolean = false) => {
  if (type === Boolean && !disableBoolean) {
    return false;
  }
  if (type === String) {
    return "";
  }
  return void 0;
};
const COMMON_PROPS = {
  ...ARIAL_PROPS.reduce((acc, item) => ({
    ...acc,
    [item.key]: {
      type: item.type,
      default: getPropsDefault(item.type)
    }
  }), {}),
  customStyle: { type: [String, Object], default: "" }
};
const toComponent = function(options) {
  if (!options.properties && options.props) {
    options.properties = options.props;
  }
  if (options.properties) {
    Object.keys(options.properties).forEach((k) => {
      let opt = options.properties[k];
      if (!uni_modules_tdesignUniapp_components_common_validator.isPlainObject(opt)) {
        opt = { type: opt };
      }
      options.properties[k] = opt;
    });
  }
  if (!options.methods)
    options.methods = {};
  if (!options.lifetimes)
    options.lifetimes = {};
  const oldCreated = options.created;
  const { controlledProps = [] } = options;
  options.created = function(...args) {
    if (oldCreated) {
      oldCreated.apply(this, args);
    }
    controlledProps.forEach(({ key }) => {
      const defaultKey = getDefaultKey(key);
      const tDataKey = getInnerControlledValue(key);
      this[tDataKey] = this[key];
      if (this[key] == null) {
        this._selfControlled = true;
      }
      if (this[key] == null && this[defaultKey] != null) {
        this[tDataKey] = this[defaultKey];
      }
    });
  };
  options.methods._trigger = function(evtName, detail, opts) {
    const target = controlledProps.find((item) => item.event === evtName);
    if (target) {
      const { key } = target;
      if (this._selfControlled) {
        const tDataKey = getInnerControlledValue(key);
        this[tDataKey] = detail[key];
      }
      this.$emit(
        `update:${key}`,
        detail[key],
        opts
      );
    }
    this.$emit(
      evtName,
      detail,
      opts
    );
  };
  return options;
};
function sortPropsType(type) {
  if (!Array.isArray(type)) {
    return type;
  }
  type.sort((a, b) => {
    if (a === Boolean) {
      return -1;
    }
    if (b === Boolean) {
      return 1;
    }
    return 0;
  });
  return type;
}
function filterProps(props, controlledProps) {
  const newProps = {};
  const emits = [];
  const reg = /^on[A-Z][a-z]/;
  const controlledKeys = Object.values(controlledProps).map((item) => item.key);
  const unControlledKeys = controlledKeys.map((key) => getDefaultKey(key));
  Object.keys(props).forEach((key) => {
    const curType = props[key].type || props[key];
    if (reg.test(key) && props[key].type === Function) {
      const str = key.replace(/^on/, "");
      const eventName = str.charAt(0).toLowerCase() + str.slice(1);
      emits.push(...[uni_modules_tdesignUniapp_components_common_utils.hyphenate(eventName), eventName]);
    } else if (controlledKeys.indexOf(key) > -1 || unControlledKeys.indexOf(key) > -1) {
      const newType = Array.isArray(curType) ? curType : [curType];
      newProps[key] = {
        type: [null, ...newType],
        default: null
      };
    } else if ([Boolean, String].indexOf(props[key].type) > -1 && props[key].default === void 0) {
      newProps[key] = {
        ...props[key],
        default: getPropsDefault(props[key].type, true)
      };
    } else {
      newProps[key] = {
        ...typeof props[key] === "object" ? props[key] : {},
        type: sortPropsType(curType)
      };
    }
  });
  return {
    newProps,
    emits
  };
}
const getEmitsByControlledProps = (controlledProps) => Object.values(controlledProps).map((item) => `update:${item.key}`);
const uniComponent = function(info) {
  const { newProps, emits } = filterProps(info.props || {}, info.controlledProps || {});
  info.props = {
    ...getExternalClasses(info),
    ...newProps,
    ...COMMON_PROPS
  };
  info.emits = Array.from(/* @__PURE__ */ new Set([
    ...info.emits || [],
    ...getEmitsByControlledProps(info.controlledProps || {}),
    ...emits
  ]));
  info.options = {
    ...info.options || {},
    multipleSlots: true
  };
  if (uni_modules_tdesignUniapp_components_common_version.canUseVirtualHost() && info.options.virtualHost == null) {
    info.options.virtualHost = true;
  }
  if (!info.options.styleIsolation) {
    info.options.styleIsolation = "shared";
  }
  if (info.name) {
    info.name = uni_modules_tdesignUniapp_components_common_utils.toPascal(info.name);
  }
  const obj = toComponent(info);
  return obj;
};
function getExternalClasses(info) {
  if (!info.externalClasses) {
    return {};
  }
  const { externalClasses } = info;
  const list = Array.isArray(externalClasses) ? externalClasses : [externalClasses];
  return list.reduce((acc, item) => ({
    ...acc,
    [uni_modules_tdesignUniapp_components_common_utils.toCamel(item)]: {
      type: String,
      default: ""
    }
  }), {});
}
exports.uniComponent = uniComponent;
//# sourceMappingURL=../../../../../../.sourcemap/mp-weixin/uni_modules/tdesign-uniapp/components/common/src/instantiationDecorator.js.map
