import type { App, Component } from "vue";

import { Select, DatePicker, DateRangePicker } from "antdv-next";
import { defineComponent, h } from "vue";

import { appDefaultSettings } from "@/settings";

type AttrMap = Record<string, unknown>;

const withAllowClearDefault = (
  name: string,
  component: Component,
  getDefaultAllowClear: () => boolean,
) => {
  return defineComponent({
    name,
    inheritAttrs: false,
    setup(_, { attrs, slots }) {
      return () => {
        const props = attrs as AttrMap;
        const allowClear = props.allowClear ?? getDefaultAllowClear();

        return h(component, { ...props, allowClear }, slots);
      };
    },
  });
};

const SelectWithDefaults = withAllowClearDefault(
  "ASelectWithDefaults",
  Select,
  () => appDefaultSettings.select.allowClear,
);

const DatePickerWithDefaults = withAllowClearDefault(
  "ADatePickerWithDefaults",
  DatePicker,
  () => appDefaultSettings.datePicker.allowClear,
);

const RangePickerWithDefaults = withAllowClearDefault(
  "ARangePickerWithDefaults",
  DateRangePicker,
  () => appDefaultSettings.datePicker.allowClear,
);

export const registerDefaultComponentProps = (app: App) => {
  const prevWarn = app.config.warnHandler;
  app.config.warnHandler = (msg: string, ...rest: unknown[]) => {
    if (typeof msg === 'string' && msg.includes('has already been registered in target app')) return;
    if (prevWarn) (prevWarn as (...a: unknown[]) => void)(msg, ...rest);
    else console.warn(msg, ...rest);
  };
  app.component("ASelect", SelectWithDefaults);
  app.component("ADatePicker", DatePickerWithDefaults);
  app.component("ARangePicker", RangePickerWithDefaults);
  app.config.warnHandler = prevWarn;
};
