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