import { deepEqual as i } from "fast-equals";
import { actionsType as o } from "../types.js";
function s(n) {
  return {
    fireFetchCount: 0,
    options: [],
    parms: n.parms,
    loading: !1,
    queryParams: n.onChangeFilterParms ? n.onChangeFilterParms("") : {},
    isOpen: !1,
    firstOpen: !0
  };
}
function t(n) {
  return (r, e) => {
    switch (e.type) {
      case o.REFRESH:
        return {
          ...r,
          fireFetchCount: r.fireFetchCount + 1
        };
      case o.SET_PARAMS:
        if (i(r.parms, e.payload))
          return r;
        {
          const u = n ? n("") : r.queryParams;
          return {
            ...r,
            parms: e.payload,
            fireFetchCount: 0,
            firstOpen: !0,
            options: [],
            queryParams: u
          };
        }
      case o.SET_QUERY_PARAMS:
        return i(r.queryParams, e.payload) ? r : {
          ...r,
          queryParams: e.payload,
          fireFetchCount: r.fireFetchCount + 1
        };
      case o.FINISH_LOAD_OPTIONS: {
        const u = e.payload?.options ?? r.options, p = e.payload?.options ? r.firstOpen : !0;
        return {
          ...r,
          options: u,
          loading: !1,
          firstOpen: p
        };
      }
      case o.OPEN:
        return {
          ...r,
          isOpen: !0,
          fireFetchCount: r.firstOpen ? r.fireFetchCount + 1 : r.fireFetchCount,
          firstOpen: !1,
          loading: r.firstOpen ? !0 : r.loading
        };
      case o.CLOSE:
        return {
          ...r,
          isOpen: !1
        };
      default:
        return r;
    }
  };
}
export {
  t as autocompleteAsyncReducer,
  s as getInitialStateAutocompleteAsync
};
