var L = Object.defineProperty;
var D = (t, r, e) => r in t ? L(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
var h = (t, r, e) => (D(t, typeof r != "symbol" ? r + "" : r, e), e);
import { PluginStaticResource as g } from "@ibiz-template/runtime";
import { defineComponent as y, ref as O, watch as I, computed as p, getCurrentInstance as A, h as d } from "vue";
import { getCheckboxProps as P, getEditorEmits as k, useNamespace as C, useControlController as F } from "@ibiz-template/vue-util";
import { isNil as j } from "ramda";
import { CheckBoxListEditorController as w, SearchFormController as N, FormNotifyState as T } from "@ibiz-template/controller";
import { SearchFormModel as M } from "@ibiz-template/model";
const R = new g(import.meta.url);
const B = y({
  name: "CustomCheckboxList",
  props: P(),
  emits: k(),
  setup(t, {
    emit: r
  }) {
    const e = C("checkbox"), s = t.controller, n = s.model.codeList, l = O([]);
    I(() => t.data, (o) => {
      s.loadCodeList(o).then((a) => {
        l.value = a;
      });
    }, {
      immediate: !0,
      deep: !0
    });
    const f = p(() => n && n.orMode ? n.orMode.toLowerCase() : "str");
    let b = ",";
    n && n.valueSeparator && (b = n.valueSeparator);
    const m = p({
      get() {
        if (!j(t.value)) {
          if (Object.is(f.value, "num") && l) {
            const o = [], a = typeof t.value == "string" ? parseInt(t.value, 10) : t.value;
            return l.value.forEach((i) => {
              (a & i.value) === i.value && o.push(i.value);
            }), o;
          }
          if (Object.is(f.value, "str")) {
            const o = t.value;
            if (o !== "") {
              if (n) {
                const a = o.split(b);
                if (n.codeItemValueNumber)
                  for (let i = 0, c = a.length; i < c; i++)
                    a[i] = Number(a[i]);
                return a;
              }
              return o.split(",");
            }
          }
        }
        return [];
      },
      set(o) {
        let a = null;
        if (Object.is(f.value, "num")) {
          let i = 0;
          o.forEach((c) => {
            const v = typeof c == "string" ? parseInt(c, 10) : c;
            i |= v;
          }), a = i;
        } else if (Object.is(f.value, "str")) {
          const i = [];
          l.value.length > 0 && (l.value.forEach((c) => {
            o.findIndex((E) => Object.is(c.value, E)) !== -1 && i.push(c.value);
          }), a = i.join(b));
        }
        r("change", a);
      }
    }), S = (o) => {
      m.value = o;
    }, x = p(() => {
      const o = Array.isArray(m.value) ? m.value : [m.value];
      return l.value.filter((a) => o.includes(a.value)).map((a) => a.text).join("、");
    });
    return {
      ns: e,
      items: l,
      selectArray: m,
      valueText: x,
      onSelectArrayChange: S,
      c: s
    };
  },
  render(t) {
    return t("div", {
      class: [this.ns.b(), this.disabled ? this.ns.m("disabled") : "", this.readonly ? this.ns.m("readonly") : ""]
    }, [[this.readonly && this.valueText, !this.readonly && t("ICheckboxGroup", {
      props: {
        ...this.c.customProps,
        value: this.selectArray
      },
      on: {
        "on-change": this.onSelectArrayChange
      }
    }, [this.items.map((r, e) => t("i-checkbox", {
      key: e,
      attrs: {
        label: r.value,
        disabled: this.disabled || this.c.disabledList.includes(r.value)
      }
    }, [t("span", {
      class: this.ns.e("text")
    }, [r.text])]))])]]);
  }
});
class _ extends w {
  constructor() {
    super(...arguments);
    /**
     * 默认禁用项
     *
     * @type {string[]}
     * @memberof CustomCheckboxListController
     */
    h(this, "disabledList", []);
  }
  async onInit() {
    var e, s;
    if (await super.onInit(), (e = this.editorParams) != null && e.DEFAULTDISABLEDLIST)
      try {
        this.disabledList = JSON.parse((s = this.editorParams) == null ? void 0 : s.DEFAULTDISABLEDLIST);
      } catch (u) {
        this.disabledList = [];
      }
  }
}
class U extends N {
  constructor() {
    super(...arguments);
    /**
     * 指定过滤参数
     *
     * @type {IData}
     * @memberof SearchFormDynamicController
     */
    h(this, "filterParams", []);
  }
  async onInit() {
    await super.onInit();
    const { FILTERPARAMS: e } = this.controlParams;
    if (e)
      try {
        this.filterParams = JSON.parse(e);
      } catch (s) {
        this.filterParams = [];
      }
  }
  /**
   * 获取搜索参数
   *
   * @return {*}  {IParams}
   * @memberof SearchFormDynamicController
   */
  getParams() {
    const e = { ...this.data.getRequestData() };
    if (this.filterParams) {
      const s = {};
      return this.filterParams.forEach((u) => {
        Object.assign(s, { [u]: e[u] });
      }), s;
    }
    return e;
  }
  /**
   * 重置搜索表单
   *
   * @author lxm
   * @date 2022-09-22 19:09:07
   */
  async reset() {
    const e = await this.service.getDraft(this.context, this.params);
    this.filterParams.forEach((s) => {
      Object.assign(this.data, { [s]: e.data[s] });
    }), this.isLoaded = !0, Object.values(this.details).forEach((s) => {
      this.filterParams.includes(s.model.codeName) && s.formStateNotify(T.DRAFT);
    }), this.force(), await this.nerve.self.evt.asyncEmit("search");
  }
}
const q = y({
  name: "SearchFormDynamic",
  props: {
    controller: {
      type: Object,
      required: !0
    },
    modelData: {
      type: M,
      required: !0
    },
    context: {
      type: Object,
      required: !0
    },
    params: {
      type: Object,
      default: () => ({})
    }
  },
  setup(t) {
    const {
      proxy: r
    } = A(), e = F(r, () => new U(t.modelData, t.context, t.params)), s = C("search-form"), u = t.modelData.source.searchButtonStyle;
    return {
      c: e,
      ns: s,
      btntype: u
    };
  },
  render() {
    if (this.c.complete)
      return d("div", {
        class: [this.ns.b(), this.ns.m(this.btntype).toLowerCase()]
      }, [d("form-control", {
        attrs: {
          "model-data": this.modelData,
          context: this.context,
          controller: this.c
        },
        nativeOn: {
          keyup: (t) => this.c.onKeyUp(t)
        }
      }), this.c.model.source.searchButtonStyle === "NONE" ? null : d("div", {
        class: this.ns.b("buttons")
      }, [d("i-button", {
        class: this.ns.be("buttons", "search"),
        on: {
          click: () => this.c.onSearchButtonClick()
        }
      }, ["查询"]), d("i-button", {
        class: this.ns.be("buttons", "reset"),
        on: {
          click: () => this.c.reset()
        }
      }, ["重置"])])]);
  }
});
class z {
  constructor() {
    h(this, "formEditor", "CustomCheckboxList");
    h(this, "gridEditor", "CustomCheckboxList");
  }
  async createController(r, e) {
    const s = new _(r, e);
    return await s.init(), s;
  }
}
class V {
  constructor() {
    h(this, "component", "SearchFormDynamic");
  }
}
const W = {
  mateUrl: import.meta.url,
  install(t) {
    R.loadStyle(["/style.css"]), t.component("CustomCheckboxList", B), ibiz.register.editor.register(
      "EDITOR_CUSTOMSTYLE_CUSTOM_CHECKBOX_LIST",
      new z()
    ), t.component("SearchFormDynamic", q), ibiz.register.control.register(
      "SEARCHFORM_RENDER_DYNAMIC",
      new V()
    );
  }
};
export {
  W as default
};
